26
#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?

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
lal
  • 269
  • 1
  • 3
  • 3
  • Sounds like boost is not installed correctly on your system – riwalk Aug 27 '10 at 13:04
  • but my `#include #include #include using namespace std; using namespace boost; int main() { string str1; cin>>str1; //string str1(" hello world! "); to_upper(str1); cout< – lal Aug 27 '10 at 13:08
  • 5
    i found it i need to install libboost-thread package in ubuntu – lal Aug 27 '10 at 13:22
  • 1
    This is compiling and running fine in my boost thread system. So I'd have to agree that its probably a problem with your installation. Also, thanks for teaching me a new way to use boost threads. I'd been using them with classes that have the operator() defined -- that's the interpretation I got from the readings. – John Rocha Aug 28 '10 at 01:12
  • Or try to link directly `libboost_system-mgw47-mt-d-X_XX.a` found in `$(BOOST_ROOT)/state/libs/` – mr5 Dec 08 '13 at 02:37
  • 3
    I had a similar problem and determined that it was because I had 2 boosts installed on my system. Getting rid of one caused the code to compile. Hope this helps someone for which the other solutions here didn't help. – t2k32316 Oct 21 '14 at 02:53

7 Answers7

39

compile with mt tag i.e -lboost_thread-mt

Taryn
  • 242,637
  • 56
  • 362
  • 405
ankita
  • 391
  • 3
  • 2
  • or `-lboost_thread-mgwXX-mt-N_NN` where XX is your version of mgw, usually only first two (4.7.1 => 47) and Ns are your boost version, again first two (boost_1_55_0 => 1_55), so final one could look like_____:::::: `-lboost_thread-mgw47-mt-1_55` – jave.web Oct 16 '14 at 07:07
24

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.

Community
  • 1
  • 1
AlaskaJoslin
  • 760
  • 8
  • 14
19

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.

gregg
  • 1,027
  • 5
  • 6
  • 9
    For the record, it's -lboost_thread. See http://antonym.org/2009/05/threading-with-boost---part-i-creating-threads.html – JRG Jul 16 '11 at 14:28
  • 1
    @Josh: thanks for the input. See also http://www.boost.org/doc/libs/1_47_0/more/getting_started/windows.html#library-naming and http://www.boost.org/doc/libs/1_47_0/more/getting_started/unix-variants.html#link-your-program-to-a-boost-library. – gregg Jul 21 '11 at 13:58
4

Instead of

g++ -pthread -lboost_thread X.cpp

Try

g++ X.cpp -pthread -lboost_thread 
Colin McGovern
  • 105
  • 1
  • 6
  • put -lboost_thread in the end does removing the warning: undefined reference to `boost::detail::thread_data_base::~thread_data_base()' – Yin May 21 '21 at 09:34
2

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.

Gábor Bakos
  • 8,982
  • 52
  • 35
  • 52
0

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

Carlosio
  • 409
  • 4
  • 6
0

add compile option

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

gregg's answer is right!

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Kevin Zhao
  • 611
  • 1
  • 5
  • 2