c++cmakeboost-log

C++ Using Boost logging with Cmake fails to compile


I am trying to compile C++ using boost logging library with CMake.

Heres a simple example my CPP file - logtests.cpp

#include <iostream>

#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/expressions.hpp>

namespace logging = boost::log;

void init_logging()
{
    logging::add_file_log("sample.log");

    logging::core::get()->set_filter
    (
        logging::trivial::severity >= logging::trivial::info
    );
}

int main(int, char*[])
{
    init_logging();

    BOOST_LOG_TRIVIAL(trace) << "This is a trace severity message";
    BOOST_LOG_TRIVIAL(debug) << "This is a debug severity message";
    BOOST_LOG_TRIVIAL(info) << "This is an informational severity message"; 
    BOOST_LOG_TRIVIAL(warning) << "This is a warning severity message";
    BOOST_LOG_TRIVIAL(error) << "This is an error severity message";
    BOOST_LOG_TRIVIAL(fatal) << "and this is a fatal severity message";
    std::cin.get();
    return 0;

if I use g++ with the following arguments it compiles and runs without an issue.

#g++ includes/screwAround/logtest.cpp -DBOOST_LOG_DYN_LINK -o logtest -lboost_log -lboost_system -lboost_thread -pthread

But if I use this CMakeLists.txt it fails.

cmake_minimum_required(VERSION 3.6.3)
project(logtest)

# Enable C+11
#set(CMAKE_CXX_STANDARD 11)
ADD_DEFINITIONS(-DBUILD_SHARED_LIBS=ON)
# Library source files

ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK)
FIND_PACKAGE(Boost 1.67 COMPONENTS log thread system log_setup filesystem REQUIRED)

set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)


INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})

# Create example executable
add_executable(logtestcase logtest.cpp)
TARGET_LINK_LIBRARIES(logtestcase ${Boost_LOG_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} )

with this error:

/usr/bin/ld: CMakeFiles/logtestcase.dir/logtest.cpp.o: undefined reference to symbol '_ZN5boost6detail12get_tss_dataEPKv'
/usr/bin/ld: //lib/arm-linux-gnueabihf/libboost_thread.so.1.67.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/logtestcase.dir/build.make:85: logtestcase] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/logtestcase.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

The root cause seems to be that CMake won't include the Threads package, I've tried added Threads::Threads to the target_link_libraries like shown here without success.


Solution

  • If you want to use dynamic multi-threaded Boost libraries, you need to set the Boost_USE_STATIC_LIBS and Boost_USE_MULTITHREAD flags before looking for Boost:

    set(Boost_USE_STATIC_LIBS OFF)
    set(Boost_USE_MULTITHREAD ON)
    FIND_PACKAGE(Boost log log_setup)
    

    These flags are documented.


    I also recommend you use the Boost::<component> imported targets when linking:

    TARGET_LINK_LIBRARIES(logtestcase Boost::log)