5

I am trying to add multithreading into my library, so I am working on creating a thread executor for my library. For this I am using boost threads.

This is the error I am getting when running a test case that links to the library:

symbol lookup error: libmylibexample.so.0: undefined symbol: _ZTVN5boost6detail16thread_data_baseE

This is the line of code in my shared library that is causing the error:

MyNameSpace::Producer producer = MyNameSpace::Producer();
threads.create_thread(boost::bind(&MyNameSpace::Producer::run, &producer));

I am compiling the library using autotools and libtool. The code compiles fine. I then create a test case that I am trying to reference the library. Here is the compilation order for compiling the test case:

g++ -I.  -I../include -g -O2 -MT runTest-runTest.o -MD -MP -MF .deps/runTest-runTest.Tpo -c -o runTest-runTest.o `test -f 'runTest.cc' || echo './'`runTest.cc

and this is my linking stage:

mv -f .deps/runTest-runTest.Tpo     .deps/runTest-runTest.Po

/bin/bash ../libtool --tag=CXX   --mode=link g++  -g -O2 ../libmylibexample/libmylibexample.la   -o runTest runTest-runTest.o -lboost_system -lboost_filesystem -lboost_regex -lboost_thread-mt -lfftw3 -ltiff

libtool: link: g++ -g -O2 -o .libs/runTest runTest-runTest.o  ../libmylibexample/.libs/libmylibexample.so -lboost_system -lboost_filesystem -lboost_regex -lboost_thread-mt -lfftw3 /usr/lib/x86_64-linux-gnu/libtiff.so

One of my colleagues suggested initializing some boost templates relating to threading to help the shared library to load the symbol from the boost_thread library. I am not entirely certain the best method to do this and if this it the right way of making things get loaded.

So to wrap things up: The error appears to involve not being able to load a symbol defined in libboost_thread from my shared library.

Jameshobbs
  • 524
  • 7
  • 17

1 Answers1

5

As the error indicates, you need to link libmylibexample with libboost_thread.

ldav1s
  • 15,885
  • 2
  • 53
  • 56
  • 1
    Yes, I see that you have them both in the linking step for `runTest`. However, you need `-lboost_thread` or `-lboost_thread-mt` at the link step you have not shown -- the link step for `libmylibexample`. – ldav1s Jan 27 '14 at 20:00
  • In my linking stage I do add both libraries `../libmylibexample/.libs/libmylibexample.so` and `-lboost_thread-mt` ... I have also tried `-lboost_thread` ... Unless there is a way to add this linking when compiling the library itself.... (Had to readd.) I will edit my post to include the library creation. Did not realize I would have to link libraries when creating the shared library itself. Usually I link all libraries together for the main application that uses all the libraries. – Jameshobbs Jan 27 '14 at 20:03
  • Okay I added the boost_thread lib to my shared library and everything worked! Thank you for the help! I am very new at creating shared libraries especially with boost. Thanks again! – Jameshobbs Jan 27 '14 at 20:05
  • 1
    When linking _shared_ libraries (DSO), it's usually not necessary to link the main application to _all_ the dependent libraries, just to the libraries it uses directly. As you discovered, each shared library must be linked to it's direct dependencies, so it can be loaded at runtime. In the _static_ linking model linking the main application with all its dependent libraries is _required_, and would be as you had it. – ldav1s Jan 27 '14 at 20:33