Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link boost libraries properly in Linux

Tags:

c++

linker

boost

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

like image 783
Athan Avatar asked Jun 20 '13 05:06

Athan


People also ask

How do I link my Boost library to code blocks?

Include Boost headers and link with Boost librarieshpp> in your source file. In your project's build options, highlight the root of your project, select the "Linker settings" tab, and add "boost_*-mgwXX-mt-1_47" to your Link libraries.

How do I add a Boost library?

In the properties dialog, select "Configuration Properties" and then "VC++ Directories". You will need to add the Boost include path to the "Include Directories" list. If you're using all header-only libraries then you're done. Otherwise, you will need to add the Boost library path to "Library Directories".

How do I update my Boost library?

Download the latest version (1.43. 0) of the Boost libraries from the Boost website and follow the steps in the getting started guide, which explains how to build Boost on a number of platforms, including Linux.


2 Answers

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.

like image 116
Athan Avatar answered Sep 25 '22 21:09

Athan


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.

like image 27
Yuushi Avatar answered Sep 21 '22 21:09

Yuushi