c++ - Boost 库在 Windows 上未正确链接

标签 c++ c++11 boost c++14

我有一个大型项目,该项目最初是在 Mac 上构建的,我正试图在 Windows 上进行编译。项目的makefile如下:

CC=C:/cygwin64/bin/g++.exe


BASE_FLAGS = -std=c++14

LDFLAGS = -I/usr/local/Cellar/boost/1.69.0/include -I/opt/local/include -I include/

LLIBFLAGS = -L/usr/local/Cellar/boost/1.68.0/lib

LINKFLAGS = -lboost_thread -lboost_system -l boost_filesystem -l boost_regex -ll -lm

FLAGS = $(BASE_FLAGS) $(LLIBFLAGS) $(LDFLAGS) $(LINKFLAGS)

rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))

SRCS := $(call rwildcard,src/,*.cpp) $(call rwildcard,src/,*.c) 

mac:
    cd src; bison -d parse.y --debug -t; flex lex.l
    time ./runscripts.sh
    $(CC) $(FLAGS) -o main.out $(SRCS)


BASE_FLAGS_WIN = -std=gnu++11

LDFLAGS_WIN = -I include/ -I C:\Users\elllz\Downloads\boost_1_69_0

LLIBFLAGS_WIN = -LC:\Users\elllz\Downloads\boost_1_69_0\stage\lib

LINKFLAGS_WIN =  -lm -lboost_system -lboost_regex -lboost_filesystem -lboost_thread

FLAGS_WIN = $(BASE_FLAGS_WIN) $(LDFLAGS_WIN)  $(LLIBFLAGS_WIN)  $(LINKFLAGS_WIN) 

win:
    bison -d .\src\parse.y -t -g
    flex .\src\lex.l 
    .\scripts\ALL_HEADER_TOKENS.bat
    cp .\parse.tab.h .\src\parse.tab.h
    cp .\parse.tab.c .\src\parse.tab.c
    cp .\lex.yy.c .\src\lex.yy.c
    rm .\parse.tab.h .\parse.tab.c .\lex.yy.c
    $(CC) $(FLAGS_WIN)  $(SRCS)  


请注意,第一秒是(工作中的)macos 目标,后面是 Windows 目标。在 Windows 上尝试链接 boost 时,我不断收到大量 undefined reference 错误,例如:

/cygdrive/c/Users/elllz/AppData/Local/Temp/cc1jUol1.o:FileVerification.cpp:(.text$_ZN5boost10filesystem11path_traits7convertEPKcS3_RSbIwSt11char_traitsIwESaIwEE[_ZN5boost10filesystem11path_traits7convertEPKcS3_RSbIwSt11char_traitsIwESaIwEE]+0x15): undefined reference to `boost::filesystem::path::codecvt()'
/cygdrive/c/Users/elllz/AppData/Local/Temp/cc1jUol1.o:FileVerification.cpp:(.text$_ZN5boost10filesystem11path_traits7convertEPKcS3_RSbIwSt11char_traitsIwESaIwEE[_ZN5boost10filesystem11path_traits7convertEPKcS3_RSbIwSt11char_traitsIwESaIwEE]+0x15): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `boost::filesystem::path::codecvt()'
/cygdrive/c/Users/elllz/AppData/Local/Temp/cc1jUol1.o:FileVerification.cpp:(.text$_ZN5boost10filesystem11path_traits7convertEPKcS3_RSbIwSt11char_traitsIwESaIwEE[_ZN5boost10filesystem11path_traits7convertEPKcS3_RSbIwSt11char_traitsIwESaIwEE]+0x32): undefined reference to `boost::filesystem::path_traits::convert(char const*, char const*, std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >&, std::codecvt<wchar_t, char, _mbstate_t> const&)'
/cygdrive/c/Users/elllz/AppData/Local/Temp/cc1jUol1.o:FileVerification.cpp:(.text$_ZN5boost10filesystem11path_traits7convertEPKcS3_RSbIwSt11char_traitsIwESaIwEE[_ZN5boost10filesystem11path_traits7convertEPKcS3_RSbIwSt11char_traitsIwESaIwEE]+0x32): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `boost::filesystem::path_traits::convert(char const*, char const*, std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >&, std::codecvt<wchar_t, char, _mbstate_t> const&)'
/cygdrive/c/Users/elllz/AppData/Local/Temp/cc1jUol1.o:FileVerification.cpp:(.text$_ZN5boost10filesystem6existsERKNS0_4pathE[_ZN5boost10filesystem6existsERKNS0_4pathE]+0x1e): undefined reference to `boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)'
/cygdrive/c/Users/elllz/AppData/Local/Temp/cc1jUol1.o:FileVerification.cpp:(.text$_ZN5boost10filesystem6existsERKNS0_4pathE[_ZN5boost10filesystem6existsERKNS0_4pathE]+0x1e): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)'
/cygdrive/c/Users/elllz/AppData/Local/Temp/cc1jUol1.o:FileVerification.cpp:(.text$_ZN5boost10filesystem15is_regular_fileERKNS0_4pathE[_ZN5boost10filesystem15is_regular_fileERKNS0_4pathE]+0x1e): undefined reference to `boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)'
/cygdrive/c/Users/elllz/AppData/Local/Temp/cc1jUol1.o:FileVerification.cpp:(.text$_ZN5boost10filesystem15is_regular_fileERKNS0_4pathE[_ZN5boost10filesystem15is_regular_fileERKNS0_4pathE]+0x1e): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)'
/cygdrive/c/Users/elllz/AppData/Local/Temp/ccbzCEFH.o:Token.cpp:(.text$_ZN5boost11regex_matchIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS5_EEEcNS_12regex_traitsIcNS_14c_regex_traitsIcEEEEEEbT_SD_RNS_13match_resultsISD_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsE[_ZN5boost11regex_matchIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS5_EEEcNS_12regex_traitsIcNS_14c_regex_traitsIcEEEEEEbT_SD_RNS_13match_resultsISD_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsE]+0x7e): undefined reference to `boost::re_detail_106900::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, boost::regex_traits<char, boost::c_regex_traits<char> > >::match()'
/cygdrive/c/Users/elllz/AppData/Local/Temp/ccbzCEFH.o:Token.cpp:(.text$_ZN5boost11regex_matchIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS5_EEEcNS_12regex_traitsIcNS_14c_regex_traitsIcEEEEEEbT_SD_RNS_13match_resultsISD_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsE[_ZN5boost11regex_matchIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS5_EEEcNS_12regex_traitsIcNS_14c_regex_traitsIcEEEEEEbT_SD_RNS_13match_resultsISD_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsE]+0x7e): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `boost::re_detail_106900::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, boost::regex_traits<char, boost::c_regex_traits<char>
> >::match()'
/cygdrive/c/Users/elllz/AppData/Local/Temp/ccbzCEFH.o:Token.cpp:(.text$_ZN5boost11basic_regexIcNS_12regex_traitsIcNS_14c_regex_traitsIcEEEEE6assignEPKcS7_j[_ZN5boost11basic_regexIcNS_12regex_traitsIcNS_14c_regex_traitsIcEEEEE6assignEPKcS7_j]+0x31): undefined reference to `boost::basic_regex<char, boost::regex_traits<char, boost::c_regex_traits<char> > >::do_assign(char const*, char const*, unsigned int)'
/cygdrive/c/Users/elllz/AppData/Local/Temp/ccbzCEFH.o:Token.cpp:(.text$_ZN5boost11basic_regexIcNS_12regex_traitsIcNS_14c_regex_traitsIcEEEEE6assignEPKcS7_j[_ZN5boost11basic_regexIcNS_12regex_traitsIcNS_14c_regex_traitsIcEEEEE6assignEPKcS7_j]+0x31): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `boost::basic_regex<char, boost::regex_traits<char, boost::c_regex_traits<char> > >::do_assign(char const*, char const*, unsigned int)'

这只是一个片段,我能找到的所有信息都说这是一个 boost 链接问题,但我终究无法确定。我正在使用以下命令编译 boost 1.69.0:

.\boostrap.bat

./bjam

./bjam install

以与找到的说明类似的方式here .我已经尝试了所有我能想到的方法,并且花了很多时间重新编译 boost,所以如果您能提供任何帮助,我将不胜感激!

我尝试过的事情:

  • 检查库是否存在于/stage/lib 中,它们确实存在,如 .a 和 .dll.a 文件。
  • 将整个 boost 文件夹移动到我的构建文件夹的相对路径中,以防我指定的链接/包含文件夹错误。
  • 使用大量选项重新编译 boost,我无法将它们全部记忆起来。

最佳答案

我怀疑您可能有多个问题...

您的 makefile 指定了不同版本的 boost 库:1.69.01.68.0

LDFLAGS = -I/usr/local/Cellar/boost/1.69.0/include -I/opt/local/include -I include/

LLIBFLAGS = -L/usr/local/Cellar/boost/1.68.0/lib

可以这么简单。

但是,在 Windows 机器上,命令:

.\boostrap.bat

通常假定使用 Visual Studio 构建 boost 库。 因此,根据您构建 boost 库的位置,您可能构建了 Visual Studio lib 文件,而不是 g++ lib 文件。您可以从库文件名中看出:

// gcc, actually mingw73: mgw73
libboost_filesystem-mgw73-mt-x64-1_69.dll.a

// msvc 2017: vc141
boost_filesystem-vc141-mt-x64-1_69.lib

听起来你的是正确的,但你可以在构建boost时明确指定g++编译器,例如:

.\boostrap.bat gcc

最后,很难维护跨平台的makefiles,尤其是boost
我建议使用 cmake相反。

关于c++ - Boost 库在 Windows 上未正确链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55191234/

相关文章:

c++ - boost::spirit 使用 std::array 解析为结构

c++ - 排列可 move 结构

drupal - drupal 的最佳缓存器是什么(Boost、xcache 或 varnish)?

c++ - 运行 boost bcp 工具时出错 : "The Boost path appears to have been incorrectly set"

c++ - 将 boost::thread 与 C++11 std::mutex 混合使用是否安全?

c++ - 为什么我不能直接在临时对象上调用 operator() ?

c++ - C++ 函数中的 Bad_alloc 异常

C++ 类模板(带默认值)

java - OpenGL 版本在 ContextAttribs() 值更改时更改

c++11 - 实现具有 const 正确性的可变参数 zip 函数