在Cygwin上链接Boost库

Linking Boost library on Cygwin

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

我已经在这里待了几个小时了,所以我来这里寻求帮助。 很确定我几乎已经弄清楚了,但我继续遇到对boost::system::generic_categoryboost::system::system_category的未定义引用的链接器错误。

我只有一个文件正在尝试链接以制作可执行文件。

我从编译它开始制作一个对象文件:

g++ -c main.cpp -I C:/boost/boost_1_61_0

这将成功创建 main.o。

我的下一个也是最后一个目标是将其链接到可执行文件。我尝试了与在其他帖子上阅读的不同的东西:

g++ main.o -L C:/boost/boost_1_61_0/stage/lib

g++ main.o -L C:/boost/boost_1_61_0/stage/lib/libboost_system.a

g++ main.o -lboost_system

结果要么告诉我它找不到库,要么类似:

main.o:main.cpp:(.text+0x89): undefined reference to `boost::system::generic_category()'
main.o:main.cpp:(.text+0x89): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `boost::system::generic_category()'
main.o:main.cpp:(.text+0x95): undefined reference to `boost::system::generic_category()'
main.o:main.cpp:(.text+0x95): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `boost::system::generic_category()'
main.o:main.cpp:(.text+0xa1): undefined reference to `boost::system::system_category()'
main.o:main.cpp:(.text+0xa1): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `boost::system::system_category()'
main.o:main.cpp:(.text$_ZN5boost11this_thread9sleep_forERKNS_6chrono8durationIlNS_5ratioILl1ELl1000000000EEEEE[_ZN5boost11this_thread9sleep_forERKNS_6chrono8durationIlNS_5ratioILl1ELl1000000000EEEEE]+0x24): undefined reference to `boost::this_thread::hiden::sleep_for(timespec const&)'
main.o:main.cpp:(.text$_ZN5boost11this_thread9sleep_forERKNS_6chrono8durationIlNS_5ratioILl1ELl1000000000EEEEE[_ZN5boost11this_thread9sleep_forERKNS_6chrono8durationIlNS_5ratioILl1ELl1000000000EEEEE]+0x24): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `boost::this_thread::hiden::sleep_for(timespec const&)'
collect2: error: ld returned 1 exit status

我知道我正确地构建了 boost 库,因为在 stage/lib 目录中的许多其他库中都有一个 libboost_system.a 文件。有什么想法吗?

让我们从您尝试过的命令开始。

g++ main.o -L C:/boost/boost_1_61_0/stage/lib

这会告诉g++C:/boost/boost_1_61_0/stage/lib目录中查找库。它没有说要拉入哪些库,但是一旦你这样做了,g++就会在那里看。

由于您的代码具有对boost_system中找到的内容(如boost::system::generic_category(的引用,并且由于您没有告诉链接器拉入该库,因此这些引用最终未定义。

g++ main.o -L C:/boost/boost_1_61_0/stage/lib/libboost_system.a

这会告诉g++C:/boost/boost_1_61_0/stage/lib/libboost_system.a目录中查找库。由于这(大概(不是一个目录,因此-L标志没有实际效果。

g++ main.o -lboost_system

这会告诉g++boost_system库中链接。虽然链接器知道如何转换库名称(例如boost_system( 到相应的文件名(例如libboost_system.a(,没有指示在哪里可以找到此文件。因此,链接器将查找它知道的默认目录。当在那里找不到文件时,g++抱怨找不到库。


此时,您应该看到需要组合的两个部分:告诉链接器要拉入哪个库以及在哪里找到它。

g++ main.o -lboost_system -L C:/boost/boost_1_61_0/stage/lib