2

I have a problem with boost. After I installed boost with following command

sudo apt-get install python-dev 
sudo apt-get install libboost-python1.54 
sudo apt-get install libboost-system1.54 libboost-filesystem1.54 
tar -zxf Boost-2014.10.tar.gz 
cd ~/build-2014.10/ 
./bootstrap.sh 
sudo ./b2 install -j8 --prefix=/usr --libdir=/usr/lib --includedir=/usr/include --build-type=minimal variant=release --layout=tagged threading=single threading=multi

boost version is 1.57. Then I run an example to test. code like

#include <iostream>
using namespace std;
#include <boost/lexical_cast.hpp>
#include <boost/filesystem.hpp>
int main(){
    cout<<"hello"<<endl;
     int a=boost::lexical_cast<int>("123456");
     cout<<"boost "<<a<<endl;
     return 1;} 

Then I compile it, error shows

g++ test -o test.cpp -lboost_system
/usr/bin/ld: cannot find -lboost_system
collect2: error: ld returned 1 exit status

If I remove line

#include <boost/filesystem.hpp>

and compile it with

g++ test -o test.cpp it works.

How to solve it?

3 Answers 3

3

Make sure you understand the difference between header files and libraries.

Header files (like /usr/include/boost/filesystem.hpp) is what you use in your source code as part of your #include directive. The C++ preprocessor reads that file and adds a bunch of declarations to your program.

A library is a compiled collection of various functions, static data and other stuff. When you use parameter -lboost_system you tell the compiler "Compile my program and link it with library libboost_system".

Your linker complains that it can't find that library (/usr/bin/ld: cannot find -lboost_system). The likely reason is that script ./bootstrap.sh did not install boost in the proper directories.

From here on you have a couple of options.

If you want to stick to Boost-2014.10.tar.gz you are on your own.

I would recommend installing package libboost-dev. It will install whatever is the current version of boost for your system and will put all files in the proper places.

And finally: when you removed the #include line from your file it worked but only because your program does not use any functionality from boost::system. If you were to use any boost classes/functions you would get compile errors if you don't have proper headers included.

5
  • Thank you for replying and explaining. I forgot to write the line 'sudo apt-get install libboost-dev'. And before install libboost-dev, I can not even find file boost/lexical_cast.hpp after I install Boost-2014.10.tar.gz!!! I am start to think that it is the include path problem. But after I install libboost-dev, it still can not find -lboost_system, Why is that?
    – Zhao Hao
    Jan 20, 2016 at 6:19
  • Please provide the output of commands dpkg -l | grep boost-system and find /usr/lib | grep boost_system
    – sмurf
    Jan 20, 2016 at 6:41
  • dpkg -l | grep boost-system outcome ii libboost-system1.58.0:amd64 1.58.0+dfsg-3.1ubuntu1 amd64 Operating system (e.g. diagnostics support) library and find /usr/lib | grep boost_system outcome /usr/lib/x86_64-linux-gnu/libboost_system.so.1.58.0 There is something, but not the version1.57 I installed. It's 1.58. My installation has problem that's for sure.
    – Zhao Hao
    Jan 20, 2016 at 7:06
  • Unless you have a valid reason to stick to 1.57 I suggest you uninstall it and leave only the ubuntu-supplied 1.58.
    – sмurf
    Jan 20, 2016 at 23:23
  • (helped me w/RHEL Based cluster problems) Important to note that the files have key terms we can search for as highlighted above. We can relate the linker command to a string. Here the middle term is the string you can search for with find or locate -lboost_system -> libboost_system* -> /usr/bin/ld -lboost_filesystem -> libboost_filesystem* -> /usr/bin/ld I included the astrix above because it may have slightly different names, but if your trying to find where a package manager put something or if everything is still installed after multiple installs this is helpful.
    – LeeRuns
    Aug 1, 2018 at 18:54
1

Order of arguments seems to be important, provided that boost package was previously correctly installed with

apt-get install -y libboost-dev

For example, when compiled like this (cpp files first, then list of libs), no issue

g++ app.cpp -std=c++11 -lboost_system -lboost_filesystem

But the command below causes errors

g++ -std=c++11 -lboost_system -lboost_filesystem  app.cpp
g++ -std=c++11 -lboost_system -lboost_filesystem  app.cpp
/tmp/cc3kjjNn.o: In function `__static_initialization_and_destruction_0(int, int)':
app.cpp:(.text+0x9b): undefined reference to `boost::system::generic_category()'
app.cpp:(.text+0xa7): undefined reference to `boost::system::generic_category()'
app.cpp:(.text+0xb3): undefined reference to `boost::system::system_category()'
/tmp/cc3kjjNn.o: In function `boost::filesystem::exists(boost::filesystem::path const&)':
app.cpp:(.text._ZN5boost10filesystem6existsERKNS0_4pathE[_ZN5boost10filesystem6existsERKNS0_4pathE]+0x19): undefined reference to `boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)'
collect2: error: ld returned 1 exit status

Slightly different order of arguments causes different error

g++  -std=c++11  -lboost_system app.cpp -lboost_filesystem
/usr/bin/ld: /tmp/cc5p9PJh.o: undefined reference to symbol '_ZN5boost6system15system_categoryEv'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libboost_system.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

Also -lboost_xyz should not include the .so suffix, otherwise

/usr/bin/ld: cannot find -lboost_filesystem.so

I hope this helps someone.

0
$ sudo apt policy libboost-* | grep system

$ sudo apt policy libboost-* | grep filesystem

$ sudo apt install libboost-system-dev

$ sudo apt install libboost-filesystem-dev

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .