简体   繁体   English

在Cygwin上链接Boost库

[英]Linking Boost library on Cygwin

I have been at this for several hours now so I have come here to ask for help. 我已经在这里待了几个小时,所以我来这里寻求帮助。 Pretty sure I almost have it figured out but I continue to have linker errors of undefined references to boost::system::generic_category and boost::system::system_category . 可以肯定的是,我几乎已经弄清楚了,但是我仍然boost::system::generic_categoryboost::system::generic_categoryboost::system::system_category的未定义引用的链接器错误。

I have just one file I am trying to link to make an executable. 我只有一个文件,我试图链接以使其成为可执行文件。

I began with compiling it to make an object file: 我首先将其编译为目标文件:

g++ -c main.cpp -IC:/boost/boost_1_61_0

This successfully creates main.o. 这样成功创建了main.o。

My next and final objective is to link it to an executable. 我的下一个也是最后一个目标是将其链接到可执行文件。 I have tried different things from what I have read on other posts: 我尝试了与其他文章不同的方法:

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

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

g++ main.o -lboost_system

The result either tells me it can't find the library or something like: 结果要么告诉我找不到该库,要么类似:

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

I know I built the boost libraries correctly as there is a libboost_system.a file along many other libraries in the stage/lib directory. 我知道我正确构建了Boost库,因为在stage / lib目录中还有一个libboost_system.a文件以及许多其他库。 Any ideas please? 有什么想法吗?

Let's start by looking at the commands you've tried. 让我们先看一下您尝试过的命令。

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

This tells g++ to look for libraries in the C:/boost/boost_1_61_0/stage/lib directory. 这告诉g++C:/boost/boost_1_61_0/stage/lib目录中查找库。 It doesn't say which libraries to pull in, but once you do, g++ will look there. 它没有说要引入哪些库,但是一旦完成, g++就会出现。

Since your code has references to things (like boost::system::generic_category ) found in boost_system and since you did not tell the linker to pull in that library, those references end up being undefined. 由于您的代码引用了在boost_system发现的东西(例如boost::system::generic_category ),并且由于您没有告诉链接器提取该库,因此这些引用最终是未定义的。

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

This tells g++ to look for libraries in the C:/boost/boost_1_61_0/stage/lib/libboost_system.a directory. 这告诉g++C:/boost/boost_1_61_0/stage/lib/libboost_system.a目录中查找库。 Since this is (presumably) not a directory, there is no real effect of the -L flag. 由于(大概)不是目录,因此-L标志没有实际作用。

g++ main.o -lboost_system

This tells g++ to link in the boost_system library. 这告诉g++链接到boost_system库。 While the linker knows how to convert a library name (eg boost_system ) to the corresponding file name (eg libboost_system.a ), there is no indication of where this file can be found. 虽然链接器知道如何将库名(例如boost_system )转换为相应的文件名(例如libboost_system.a ),但是没有指示可以在哪里找到该文件。 So the linker will look in the default directories it knows of. 因此,链接器将查找它知道的默认目录。 When the file is not found there, g++ complains about being unable to find the library. 如果在该文件中找不到该文件,则g++抱怨找不到该库。


At this point you should see the two pieces that need to be combined: tell the linker which library to pull in and where to find it. 在这一点上,您应该看到需要结合的两个部分:告诉链接器插入哪个库以及在哪里找到它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM