C++ 如何在 Linux 中正确链接 boost 库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17206298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:00:29  来源:igfitidea点击:

How to link boost libraries properly in Linux

c++boostlinker

提问by Athan

I've been trying to go through Boost tutorials but I got stuck at linking the filesystem library.

我一直在尝试阅读 Boost 教程,但在链接文件系统库时遇到了困难。

I have Ubuntu 12.10. Installation wasn't that hard

我有 Ubuntu 12.10。安装没那么难

sudo apt-get install libboost-all-dev

This put all headers in /usr/local/include and compiled sources in /usr/lib/
[--headers]
[--binaries]

这将所有头文件放在 /usr/local/include 中,并将编译源放在 /usr/lib/
[--headers]
[--binaries] 中

I wrote this program [--program]. When I tried to compiled it

我写了这个程序 [--program]。当我试图编译它时

 g++ -g tut1.cpp -o tut1 -lboost_system -lboost_filesystem

got this errors: [--errors].
After a little search on http://www.boost.org/doc/libs/1_53_0/more/getting_started/unix-variants.html
I tried this:

得到这个错误:[--errors]。
http://www.boost.org/doc/libs/1_53_0/more/getting_started/unix-variants.html上进行了一些搜索后,
我尝试了这个:

g++ -g -I /usr/local/include/boost/ tut1.cpp -o tut1 -L /usr/lib/libboost_filesystem.a -lboost_system -lboost_filesystem 

but didn't have luck. I had the same errors.

但没有运气。我有同样的错误。

Since I cannot put more than 2 links in the post, here are all links
http://pastebin.com/DakVFn12

由于我不能在帖子中放置超过 2 个链接,这里是所有链接
http://pastebin.com/DakVFn12

回答by Athan

I found the answer myself here:
http://www.richelbilderbeek.nl/CppLinkErrorUndefinedReferenceToBoostFilesystemDetailGet_current_path_api.htm
Looks like binaries weren't in /usr/lib but in /usr/local/lib.
So the correct command for compilation would be:

我自己在这里找到了答案:
http:
//www.richelbilderbeek.nl/CppLinkErrorUndefinedReferenceToBoostFilesystemDetailGet_current_path_api.htm 看起来二进制文件不在 /usr/lib 中,而是在 /usr/local/lib 中。
所以正确的编译命令是:

g++ -g tut1.cpp -o tut1 -L/usr/local/lib/ -lboost_filesystem

@Yuushi, that was 1 problem.

@Yuushi,这是 1 个问题。

回答by Yuushi

The -Lcommand should be the base path where the libraries are contained, not the path to a specific library. Try with -L /usr/lib/instead.

-L命令应该是包含库的基本路径,而不是特定库的路径。试试吧-L /usr/lib/