Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking boost::thread

I'm trying to learn something with boost libraries, but I get a problem when I try to compile something that includes boost::threads. I get an error during linking, this is the message:

/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/../../../../x86_64-pc-linux-gnu/bin/ld: cannot find -lboost-thread

But it's strange because this happens only when I compile with a normal user, using root I can compile with no problem.

Thanks in advance.

like image 883
alkz Avatar asked Mar 05 '12 19:03

alkz


People also ask

What is boost thread C++?

Boost. Thread is the library that allows you to use threads. Furthermore, it provides classes to synchronize access on data which is shared by multiple threads. Threads have been supported by the standard library since C++11.

Is boost :: thread header only?

Boost. DateTime: This dependency is mandatory, but even if Boost. DateTime is a non header-only library Boost. Thread uses only parts that are header-only, so in principle you should not need to link with the library.

Does boost use Pthread?

Boost. Sync supports building against one of the following underlying APIs: pthreads or Windows API. On most platforms pthreads are the only option. On Windows, Windows API is used by default, but pthreads can be enabled.


2 Answers

Include

#include <boost/thread/thread.hpp>

Other Linker Flags

-lboost_system -lboost_thread-mt
like image 142
neoneye Avatar answered Sep 27 '22 15:09

neoneye


check the lib name in boost install path (default: /usr/lib/), if it's libboost_thread.so, add -lboost_thread. Don't forget to specify the path to boost directory with -L/usr/lib/boost. If it only work as root, check your privilege in this directory :

ls -la /usr/lib/ | grep boost

you should see your login, and rw_r_r_ (check you have the read permission).

If you have this permission on the directory and on the boost lib, linking with gcc can be done :

g++ obj.o obj2.o -L/usr/lib -lboost_thread

if you don't own files or don't have read permissions, log as root and add them

chown -R /usr/lib <your login>
chmod +r /usr/lib/lib*.so
like image 37
blaize Avatar answered Sep 27 '22 17:09

blaize