Need help linking to boost thread library

Asked by Allan W. Macdonald on 2019-11-19

uname -a
Linux Dell-Ubuntu 4.15.0-66-generic #75-Ubuntu SMP Tue Oct 1 05:24:09 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

I am trying to compile and link a very simple program named "testthread.cpp", merely intended to test my boost installation:

#include <boost/thread.hpp>
#include <iostream>

int main()
  std::cout << boost::thread::hardware_concurrency() << std::endl;

When I compile with the following command:
gcc -o testthread testthread.cpp -lboost_thread

I get the following errors:
/usr/bin/ld: /tmp/ccsW57B3.o: undefined reference to symbol '_ZN5boost6system15system_categoryEv'
//usr/lib/x86_64-linux-gnu/ error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

Boost was installed as follows:

sudo apt-get install libboost-all-dev

What am I doing wrong?


Question information

English Edit question
Ubuntu boost1.65.1 Edit question
No assignee Edit question
Solved by:
Allan W. Macdonald
Last query:
Last reply:

This question was reopened

Manfred Hampl (m-hampl) said : #1

For compiling cpp programs you shoud use g++ instead of gcc

Sorry, but if I replace gcc with g++ I get the same error.

OK I figured it out myself!

It turns out that I also needed to link the boost_system library although I'm not sure why. Here is the command line that succeeded:

g++ -o testthread testthread.cpp -lboost_system -lboost_thread

I guess it helps to read stuff! Quoted from:

 Boost.Thread depends on some non header-only libraries.
    Boost.System: This dependency is mandatory and you will need to link with the library.