boost_log example with sinks fails to compile

2019-04-22 02:20发布

问题:

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?

回答1:

You must link boost_thread manualy:

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


回答2:

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