提升库无法在Windows上正确链接

Boost Libraries not linking properly on Windows

本文关键字:链接 Windows      更新时间:2023-10-16

我有一个最初建立在MacOS上的大型项目,我正在尝试在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:UserselllzDownloadsboost_1_69_0
LLIBFLAGS_WIN = -LC:UserselllzDownloadsboost_1_69_0stagelib
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 .srcparse.y -t -g
    flex .srclex.l 
    .scriptsALL_HEADER_TOKENS.bat
    cp .parse.tab.h .srcparse.tab.h
    cp .parse.tab.c .srcparse.tab.c
    cp .lex.yy.c .srclex.yy.c
    rm .parse.tab.h .parse.tab.c .lex.yy.c
    $(CC) $(FLAGS_WIN)  $(SRCS)  

请注意,第一秒是(工作)MacOS目标,稍后是Windows目标。在尝试链接Windows上的链接时,我一直在遇到很多未定义的参考错误,例如:

/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 1.69.0:

.boostrap.bat

./bjam

./bjam install

以类似于此处的说明类似。我已经尝试了有关我能想到的一切的JSUT,并且花了很多时间重新编译,因此您可能会提供的任何帮助都将不胜感激!

我尝试过的事情:

  • 检查诽谤确实存在于/stage/lib中,它们确实存在。
  • 将整个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。