boost_log example with sinks fails to compile
Asked Answered
D

3

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?

Decal answered 10/8, 2014 at 17:36 Comment(0)
U
8

You must link boost_thread manualy:

g++ -std=c++11 boost_log_sinks.cpp -DBOOST_LOG_DYN_LINK -lboost_log -lboost_thread -lpthread
Ungovernable answered 10/9, 2014 at 2:59 Comment(1)
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_setupDecal
I
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
Interdependent answered 6/7, 2017 at 9:35 Comment(1)
It worked after I added -lboost_log_setup and -lboost_thread to the linker.Lamoreaux
A
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
Adonic answered 9/12, 2019 at 11:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.