10

I was considering using boost_log for one project and right at the beginning I faced following problem.

Boost Log Example I found at: http://www.boost.org/doc/libs/1_54_0/libs/log/example/doc/tutorial_file.cpp fails to compile. Other simpler examples (without sinks) I compile without problems.

g++ -std=c++11 boost_log_sinks.cpp -DBOOST_LOG_DYN_LINK -lboost_log -lpthread

/usr/bin/ld: /tmp/ccApJdsi.o: undefined reference to symbol '_ZN5boost6detail12get_tss_dataEPKv' //usr/lib/x86_64-linux-gnu/libboost_thread.so.1.54.0: error adding symbols: DSO missing from command line collect2: error: ld returned 1 exit status

I am working on Ubuntu14.04 my g++ version is g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2

Does anybody knows why is this happening?

alfC
  • 14,261
  • 4
  • 67
  • 118
brane
  • 585
  • 6
  • 20

3 Answers3

8

You must link boost_thread manualy:

g++ -std=c++11 boost_log_sinks.cpp -DBOOST_LOG_DYN_LINK -lboost_log -lboost_thread -lpthread
zitsen
  • 339
  • 2
  • 10
  • 3
    Hi Zitsen I still got different error but when I added two addtional libraries than it worked. Thanks. Folowing line worked for me: g++ -std=c++11 boost_log_sinks.cpp -DBOOST_LOG_DYN_LINK -lboost_log -lboost_thread -lpthread -lboost_system -lboost_log_setup – brane Sep 14 '14 at 16:07
3

The boost_log library uses features from other boost libraries. Unfortunately the documentation fails to indicate which ones are these libraries. So whenever you use one of these features, you need to link with the corresponding lib in which that feature is in or you get the error message:

undefined reference to symbol

The solution I use is to loop all boost libs to search for that symbol (in your case _ZN5boost6detail12get_tss_dataEPKv.)

In Ubuntu 17.04 the boost libs are stored in /usr/lib/x86_64-linux-gnu/libboost_*

So with following script:

for i in /usr/lib/x86_64-linux-gnu/libboost_*
do
   echo $i
   nm $i|grep _ZN5boost6detail12get_tss_dataEPKv
done

you get a list of libraries where the symbol is either used (U), e.g.:

/usr/lib/x86_64-linux-gnu/libboost_log.a
                 U _ZN5boost6detail12get_tss_dataEPKv
                 U _ZN5boost6detail12get_tss_dataEPKv
                 U _ZN5boost6detail12get_tss_dataEPKv
/usr/lib/x86_64-linux-gnu/libboost_log_setup.a
                 U _ZN5boost6detail12get_tss_dataEPKv

or defined (T), e.g.:

/usr/lib/x86_64-linux-gnu/libboost_thread.a
0000000000000740 T _ZN5boost6detail12get_tss_dataEPKv

This last one is the one you need. It tells that the symbol you are looking for is in the library libboost_thread.a. So all you have to do now is to include that lib in your link command:

g++ -std=c++11 boost_log_sinks.cpp -DBOOST_LOG_DYN_LINK -lboost_log -lboost_thread -lpthread
jav
  • 583
  • 5
  • 15
0

To compile the example https://www.boost.org/doc/libs/1_71_0/libs/log/example/doc/tutorial_file.cpp, in Ubuntu I had to use a compilation with all these libraries:

$ c++ -DBOOST_LOG_DYN_LINK tutorial_file.cpp \
-lboost_log_setup -lboost_log -lboost_thread -lpthread -lboost_system
alfC
  • 14,261
  • 4
  • 67
  • 118