linker error while linking boost log tutorial (undefined references)

Just add a line

#define BOOST_LOG_DYN_LINK 1

as the first line of boosttest.cc.

Alternatively, you can add -DBOOST_LOG_DYN_LINK to you compilation step (not the linking step, as you posted in the question):

g++ -std=c++11 -Wall -pedantic -g -O0 -DBOOST_LOG_DYN_LINK  -c boosttest.cc
g++  boosttest.o -lpthread -lboost_log -o boosttest

I also had this error, if you are using CMake you may able to resolve this issue in the following way,

  1. Finding related packages: log log_setup

    find_package(Boost COMPONENTS program_options log log_setup REQUIRED)

2.Append log and log_setup into other libs:

  set(PROJECT_LIB ${PROJECT_LIB} ${Boost_LOG_LIBRARY} ${Boost_LOG_SETUP_LIBRARY})
  1. Link those libraries into your program,

    target_link_libraries(${PROJECT_NAME} -Wl,--start-group ${PROJECT_LIB} -Wl,--end-group)

  2. Add DBOOST_LOG_DYN_LINK into CMAKE_CXX_FLAGS flags

    set(CMAKE_CXX_FLAGS "-Wall -Wextra -fPIC -fopenmp -DBOOST_LOG_DYN_LINK")


I had the same annoying problem. The macro should be defined while compiling, not linking:

g++ -std=c++11 -DBOOST_LOG_DYN_LINK -c boosttest.cc

In the linker command, make sure the order is as follows:

g++ boosttest.o -lboost_log -lpthread -o boosttest

-lboost_log_setup solved the issue for me.

I borrowed it from spdlog bench Makefile

g++ -std=c++11 -Wall -pedantic -g -O0 -DBOOST_LOG_DYN_LINK  -c boost-bench-mt.cpp
g++ boost-bench-mt.o  -lpthread -lboost_log -lboost_log_setup  -lboost_system -lboost_thread -o boost-bench-mt

Tags:

C++

Boost

Fedora

Ld