How to link boost libraries properly in Linux

2020-06-03 05:57发布

问题:

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

I have Ubuntu 12.10. Installation wasn't that hard

sudo apt-get install libboost-all-dev

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

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

 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:

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

回答1:

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:

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

@Yuushi, that was 1 problem.



回答2:

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



标签: c++ boost linker