8

I am trying to learn how to use the C++ Boost.Thread library. I have installed the Boost libraries on my Ubuntu 11.10 system. I am following the book "The Boost C++ Libraries" by Schaling - specifically example 6.1 on page 66. I am trying to compile the following code example:

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

void wait(int seconds)
{ 
  boost::this_thread::sleep(boost::posix_time::seconds(seconds));
}

void thread()
{
  for(int i = 0; i < 5; ++i)
  {
    wait(1);
    std::cout << i << std::endl;
  }
}

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

However, when I compile this with the following from the command line:

$ g++ example61.cpp -o example61 -I /usr/local/include

I get the following output:

/tmp/cc6bVu1F.o: In function `main':
example6.cpp:(.text+0x9d): undefined reference to `boost::thread::join()'
example6.cpp:(.text+0xae): undefined reference to `boost::thread::~thread()'
example6.cpp:(.text+0xc6): undefined reference to `boost::thread::~thread()'
/tmp/cc6bVu1F.o: In function `boost::detail::thread_data_base::thread_data_base()':
example6.cpp:(.text._ZN5boost6detail16thread_data_baseC2Ev[_ZN5boost6detail16thread_data_baseC5Ev]+0x24): undefined reference to `vtable for boost::detail::thread_data_base'
/tmp/cc6bVu1F.o: In function `void boost::this_thread::sleep<boost::posix_time::seconds>(boost::posix_time::seconds const&)':
example6.cpp:(.text._ZN5boost11this_thread5sleepINS_10posix_time7secondsEEEvRKT_[void boost::this_thread::sleep<boost::posix_time::seconds>(boost::posix_time::seconds const&)]+0x35): undefined reference to `boost::this_thread::sleep(boost::posix_time::ptime const&)'
/tmp/cc6bVu1F.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)':
example6.cpp:(.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/cc6bVu1F.o: In function `boost::detail::thread_data<void (*)()>::~thread_data()':
example6.cpp:(.text._ZN5boost6detail11thread_dataIPFvvEED2Ev[_ZN5boost6detail11thread_dataIPFvvEED5Ev]+0x1f): undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
/tmp/cc6bVu1F.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 don't know how to interpret this. Can anyone help? Thank you so much!

ildjarn
  • 62,044
  • 9
  • 127
  • 211
TJB
  • 3,493
  • 4
  • 23
  • 20
  • 3
    You have to link with a boost thread library – Violet Giraffe Oct 28 '11 at 20:03
  • @ Violet Giraffe can you help with how I do that? Thanks! – TJB Oct 28 '11 at 20:05
  • 1
    Did you build the Boost libraries yourself? That is the very hard way to do it. If I were you, I would uninstall everything that you installed from /usr/local/include and /usr/local/lib, and `apt-get install libboost-thread-dev` – Zan Lynx Oct 28 '11 at 20:14
  • @Zan Lynx, I had originally done that. But the Ubuntu packages are for version 1.46 while the book I am following is using 1.47. I had wanted to have the same version. Should I just use the easier way of installing and worry about differences later? Thanks for advice! – TJB Oct 28 '11 at 20:16
  • @TJB: Some of your problems are being caused by installing to an unusual directory. But if you want to keep it, the next thing you need to do is add /usr/local/lib to your shared library lookup directories. – Zan Lynx Oct 28 '11 at 20:18
  • @ZanLynx: OK. As you can tell I'm a bit of a newbie. The book mentioned /usr/local. What is the more typical location? Thanks so much for advice. – TJB Oct 28 '11 at 20:22

5 Answers5

11

That is a linking error. It means your code is correct and you include the correct headers, but the compiler doesn't link against the boost threading library. To fix this, you need to compile like this:

g++ example61.cpp -o example61 -I /usr/local/include -lboost_thread

If you've installed the Boost threading library to a non-standard path, you must also add it to the search path:

g++ example61.cpp -o example61 -I /usr/local/include -lboost_thread -L/usr/local/lib
Antti
  • 11,944
  • 2
  • 24
  • 29
  • @ Antii, this almost works. It compiles anyway. But when I run it I get the following: ./example6: error while loading shared libraries: libboost_thread.so.1.47.0: cannot open shared object file: No such file or directory – TJB Oct 28 '11 at 20:12
  • You need to pass the path to the library so that it can be found on runtime: LD_LIBRARY_PATH=/usr/local/lib ./example6 – Antti Oct 28 '11 at 20:18
  • thanks for the help. It worked. Is this a non-standard path for Boost? The documentation suggested /usr/local. Where should I put it on Ubuntu? Thanks so much! – TJB Oct 30 '11 at 22:03
  • /usr/local is a common path for storing stuff you installed yourself, but it's usually not in the standard search path of the compiler. Putting boost there is fine, but normally you'd install Boost from the Ubuntu repository, and in that case it'd be installed in /usr (and would be automatically found by the compiler). – Antti Oct 31 '11 at 08:36
  • I found that I needed to add -lboost_system also. – Vignesh Dec 20 '17 at 00:40
1

Following your comments I share with you compilation string for pocketcpp compilation tool:

g++ -static -I"\boost" "$(FULL_CURRENT_PATH)" -L"\MinGW\lib" -lboost_thread -lboost_system -o "$(CURRENT_DIRECTORY)\$(NAME_PART).exe"

Good luck,

1

Boost thread are not a template only library. You need to add a -lboost_thread while linking /compiling.

Most of the libraries in boost are implemented in headers. They can simply be included like you have done. Boost thread on the other hand, is of such a nature that you need to depend on its compiled units, only the declaration of its function are readily available to you in the header. So the compiler, or more correctly the linker, which is responsible for linking your calls to the declared functions /classes need to know where to look for these symbols. By invoking the compiler with a -lboost_thread you tell it to link to the library (-l) boost thread.

daramarak
  • 6,115
  • 1
  • 31
  • 50
1

You need to link with the library. Some Boost libraries are implemented entirely in the header files and do not need a library. But others, like thread, are implemented partly in headers and partly in compiled library code.

I believe that you need to add -lboost_thread-mt to your compile command.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
0

I commented above, but wanted to share the full command line here.

g++ -std=c++11 thread_example.cpp -lboost_thread -lboost_system

[I'm using thread_example.cpp as the source filename; please replace with your own]

Vignesh
  • 111
  • 4