Boost.Log with CMake causing undefined reference error

后端 未结 3 1553
故里飘歌
故里飘歌 2020-12-14 17:17

I am trying to use the new Boost.Log library in a project I am working on. The project is built with CMake. I am receiving link errors claiming that the linker has come acro

相关标签:
3条回答
  • 2020-12-14 17:41

    It is Boost_INCLUDE_DIRS not Boost_INCLUDE_DIR.

    You could try enable Boost_USE_STATIC_LIBS

    0 讨论(0)
  • 2020-12-14 17:45

    For CMake 3.15 (and some earlier versions) the following appears to be sufficient to build Boost.Log with CMake without the original linker error:

    cmake_minimum_required(VERSION 3.15)
    project(boost_log_tutorial)
    
    SET(Boost_USE_STATIC_LIBS ON)           # link statically
    #ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK)  # or, link dynamically
    
    find_package(Boost 1.69.0 COMPONENTS log REQUIRED)
    
    add_executable(boost_log_tutorial main.cpp)
    target_link_libraries(boost_log_tutorial Boost::log_setup Boost::log)
    

    The key things seem to be linking against Boost:log_setup and Boost::log, and specifying either static or dynamic linking with SET(Boost_USE_STATIC_LIBS ON) or ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK).

    0 讨论(0)
  • 2020-12-14 17:49

    It looks like it boils down to linking to the shared version of Boost.Log.

    There is a bit of detail on the issue in the docs for Boost.Log Your error message mentions the namespace boost::log::v2s_mt_posix and from the docs, this implies the linker is expecting to link to a static version of Boost.Log.

    If you want to link to the shared version, it seems you need to define BOOST_LOG_DYN_LINK or BOOST_ALL_DYN_LINK, i.e. in your CMakeLists.txt add:

    ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK)
    

    If you want to link to the static version of Boost.Log, instead you need to add a CMake variable before calling FIND_PACKAGE(Boost ...):

    SET(Boost_USE_STATIC_LIBS ON)
    FIND_PACKAGE(Boost 1.54 COMPONENTS log REQUIRED)
    

    For further variables which affect how CMake finds Boost, see the docs for FindBoost.

    0 讨论(0)
提交回复
热议问题