lal
lal

Reputation: 269

Boost thread error: undefined reference

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

void hello()
{
  std::cout <<
    "Hello world, I'm a thread!"
    << std::endl;
}

int main(int argc, char* argv[])
{
  boost::thread thrd(&hello);
  thrd.join();
  return 0;
}

I ran tried to compile this program, and got these errors:

/usr/include/boost/thread/pthread/mutex.hpp:40: undefined reference to
   `boost::thread_resource_error::thread_resource_error()'
/usr/include/boost/thread/pthread/mutex.hpp:40: undefined reference to 
   `boost::thread_resource_error::~thread_resource_error()'
/usr/include/boost/thread/pthread/mutex.hpp:40: undefined reference to 
   `typeinfo for boost::thread_resource_error'
./src/thread.o: In function `condition_variable':
/usr/include/boost/thread/pthread/condition_variable_fwd.hpp:33: 
  undefined reference to `boost::thread_resource_error::thread_resource_error()'
/usr/include/boost/thread/pthread/condition_variable_fwd.hpp:33: 
  undefined reference to `boost::thread_resource_error::~thread_resource_error()'
/usr/include/boost/thread/pthread/condition_variable_fwd.hpp:33: \
  undefined reference to `typeinfo for boost::thread_resource_error'
./src/thread.o: In function `thread_data_base':
/usr/include/boost/thread/pthread/thread_data.hpp:54: 
  undefined reference to `vtable for boost::detail::thread_data_base'
./src/thread.o: In function `thread<void (*)()>':
/usr/include/boost/thread/detail/thread.hpp:188: 
  undefined reference to `boost::thread::start_thread()'
./src/thread.o: In function `~thread_data':
/usr/include/boost/thread/detail/thread.hpp:40: 
  undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
/usr/include/boost/thread/detail/thread.hpp:40: undefined reference to 
  `boost::detail::thread_data_base::~thread_data_base()'

Can any one tell me why i am getting this error?

Upvotes: 26

Views: 61634

Answers (7)

Colin McGovern
Colin McGovern

Reputation: 124

Instead of

g++ -pthread -lboost_thread X.cpp

Try

g++ X.cpp -pthread -lboost_thread 

Upvotes: 4

Carlosio
Carlosio

Reputation: 479

I had the same error. I fixed it compiling with -lboost_thread

Upvotes: 0

AlaskaJoslin
AlaskaJoslin

Reputation: 790

I had the same question, but -lboost_thread-mt is now deprecated see this answer on askubuntu.com. Instead what you now want in your makefile (at least for linux) is:

-lpthread -lboost_thread ...

Boost has simply given you the responsibility to link to your system's thread library.

Upvotes: 24

Janos Erzinger
Janos Erzinger

Reputation: 21

I had a similar problem with centos 6.5 when compiling povray 3.7 and this solved it - just add -lboost_thread-mt in your Makefile.

Upvotes: 2

Kevin Zhao
Kevin Zhao

Reputation: 611

add compile option

-L<path_to_lib> -lboost-thread-gcc-xx-1_nn

gregg's answer is right!

Upvotes: 0

ankita
ankita

Reputation: 391

compile with mt tag i.e -lboost_thread-mt

Upvotes: 39

gregg
gregg

Reputation: 1027

Many boost libraries are fully implemented in header files. Boost.thread is not. It seems that it is not linking in the boost thread library. Check your linker search paths. Or, as the Stargazer712's comment on the OP says, check the installation. You should see something like libboost_thread-gcc-xxx-1_nn.o in your lib directory. If so, try referencing it explicitly in your link step (something like -L<path_to_lib> -lboost-thread-gcc-xx-1_nn). If not, then you apparently don't have a complete installation.

Upvotes: 19

Related Questions