Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking against boost_thread fails under Ubuntu 11.10

Tags:

c++

ubuntu

ld

I already updated to the new Ubuntu 11.10.The problem is now, that the software I am working on is not linking any more. I found this wiki entry which seems to describe the problem. None the less I am unable to fix it. To reproduce the problem I wrote the following small test program:

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

void blubb() {
  std::cout << "hello world" << std::endl;
}

int main() {
  boost::thread t(&blubb);
  t.join();
}

Than I use the following command to compile:

g++ -o test -lboost_thread test.cc

The output I get is the following:

/tmp/cc0O0dAC.o: In function `main':
test.cc:(.text+0x49): undefined reference to `boost::thread::join()'
test.cc:(.text+0x55): undefined reference to `boost::thread::~thread()'
test.cc:(.text+0x70): undefined reference to `boost::thread::~thread()'
/tmp/cc0O0dAC.o: In function `boost::detail::thread_data_base::thread_data_base()':
test.cc:(.text._ZN5boost6detail16thread_data_baseC2Ev[_ZN5boost6detail16thread_data_baseC5Ev]+0x24):     undefined reference to `vtable for boost::detail::thread_data_base'
/tmp/cc0O0dAC.o: In function `boost::thread::thread<void (*)()>(void (*)(), boost::disable_if<boost::is_convertible<void (*&)(), boost::detail::thread_move_t<void (* ()> >, boost::thread::dummy*>::type)':
test.cc:(.text._ZN5boost6threadC2IPFvvEEET_NS_10disable_ifINS_14is_convertibleIRS4_NS_6detail13thread_move_tIS4_EEEEPNS0_5dummyEE4typeE[_ZN5boost6threadC5IPFvvEEET_NS_10disable_ifINS_14is_convertibleIRS4_NS_6detail13thread_move_tIS4_EEEEPNS0_5dummyEE4typeE]+0x30): undefined reference to `boost::thread::start_thread()'
/tmp/cc0O0dAC.o: In function `boost::detail::thread_data<void (*)()>::~thread_data()':
test.cc:(.text._ZN5boost6detail11thread_dataIPFvvEED2Ev[_ZN5boost6detail11thread_dataIPFvvEED5Ev]+0x1f): undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
/tmp/cc0O0dAC.o:(.rodata._ZTIN5boost6detail11thread_dataIPFvvEEE[typeinfo for boost::detail::thread_data<void (*)()>]+0x10): undefined reference to `typeinfo for boost::detail::thread_data_base'
collect2: ld returned 1 exit status

I also tried to add -lpthread and -pthread in several orders with no success. This command works smoothly on an older Ubuntu version. What am I doing wrong (I have similar problems with PCRE and Google Protocol Buffers).

Thanks in advance for any help!

like image 258
Markus Pilman Avatar asked Oct 13 '11 14:10

Markus Pilman


1 Answers

Ok I mamaged to fix it, it is ridiculous:

as written in the question

g++ -o test -lboost_thread test.cc

does not work, but

g++ -o test test.cc -lboost_thread

does... So not only the order of libraries matter, but also the source files must be written before the dependencies...

like image 147
Markus Pilman Avatar answered Nov 20 '22 12:11

Markus Pilman