Need help linking to boost thread library

Asked by Allan W. Macdonald

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/libboost_system.so.1.65.1: 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?

Thanks,
Allan

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu boost1.65.1 Edit question
Assignee:
No assignee Edit question
Solved by:
Allan W. Macdonald
Solved:
Last query:
Last reply:
Revision history for this message
Manfred Hampl (m-hampl) said :
#1

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

Revision history for this message
Allan W. Macdonald (allan-w-macdonald) said :
#2

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

Revision history for this message
Allan W. Macdonald (allan-w-macdonald) said :
#3

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

Revision history for this message
Allan W. Macdonald (allan-w-macdonald) said :
#4

I guess it helps to read stuff! Quoted from: https://www.boost.org/doc/libs/1_65_1/doc/html/thread/build.html:

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