【问题标题】:C++ - Cannot link to Boost::logger using msys2 and cmakeC++ - 无法使用 msys2 和 cmake 链接到 Boost::logger
【发布时间】:2017-05-01 18:48:11
【问题描述】:

从 Boost:logger 教程构建简单示例:

#include <boost/log/trivial.hpp>

int main(int, char*[])
{
    BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
    BOOST_LOG_TRIVIAL(debug) << "A debug severity message";
    BOOST_LOG_TRIVIAL(info) << "An informational severity message";
    BOOST_LOG_TRIVIAL(warning) << "A warning severity message";
    BOOST_LOG_TRIVIAL(error) << "An error severity message";
    BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";

    return 0;
}

我的这个项目的 cmake 文件:

# Adding Boost library
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)

find_package(Boost 1.63.0
             COMPONENTS system
                        filesystem
                        log
                        unit_test_framework
             REQUIRED)

if(Boost_FOUND)
    include_directories(SYSTEM ${Boost_INCLUDE_DIRS})
endif()

# Adding main sources to build
file(GLOB PROJECT_SOURCES sources/*.cpp)
file(GLOB PROJECT_HEADERS sources/*.h)

add_executable(${PROJECT_NAME}
               ${PROJECT_SOURCES}
               ${PROJECT_HEADERS})
target_link_libraries(${PROJECT_NAME}
                      ${Boost_LIBRARIES})

我收到下一条错误消息:

-- Boost version: 1.63.0
-- Found the following Boost libraries:
--   system
--   filesystem
--   log
--   unit_test_framework
--   date_time
--   log_setup
--   thread
--   regex
--   chrono
--   atomic
-- Configuring done
-- Generating done
-- Build files have been written to: D:/path_to_build_folder/build
[ 10%] Linking CXX executable ProjectName.exe
C:/User/msys64/mingw64/lib/libboost_log-mt.a(default_sink.o):(.text$_ZN5boost16thread_exceptionC2EiPKc[_ZN5boost16thread_exceptionC2EiPKc]+0x14): undefined reference to `boost::system::system_category()'
C:/User/msys64/mingw64/lib/libboost_log-mt.a(exceptions.o):(.text+0x2601): undefined reference to `boost::system::system_category()'
C:/User/msys64/mingw64/lib/libboost_log-mt.a(exceptions.o):(.text+0x2732): undefined reference to `boost::system::system_category()'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[2]: *** [CMakeFiles\ProjectName.dir\build.make:269: ProjectName.exe] Error 1
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:67: CMakeFiles/ProjectName.dir/all] Error 2
mingw32-make.exe: *** [Makefile:94: all] Error 2

据我了解,发生错误是因为链接器无法将 libboost_log 链接到 boost 系统库。这个对吗?但系统库也应该包含在${Boost_LIBRARIES} 中。

如何解决这个问题?

从 Cmake 文件可以看出,我使用 filesystem 库来处理其他东西,并且它编译并正常工作。只有我包含日志库才会出现此问题。

【问题讨论】:

    标签: c++ logging boost cmake


    【解决方案1】:

    在我看来,您还必须添加 boost thread 库。


    无论如何,为避免此类问题,我强烈建议使用目标语法来链接 boost。

    即,而不是

    find_package(Boost 1.63.0
                 COMPONENTS system
                            filesystem
                            log
                            unit_test_framework
                 REQUIRED)
    include_directories(SYSTEM ${Boost_INCLUDE_DIRS})
    [...]
    target_link_libraries(${PROJECT_NAME}
                          ${Boost_LIBRARIES})
    

    你可以使用

    find_package(Boost 1.63.0
                 COMPONENTS log
                 REQUIRED)
    target_link_libraries(${PROJECT_NAME}
                          Boost::log)
    

    它会自动 1) 设置所需的包含目录,以及 2) 链接到 Boost::log 的依赖项。

    另外,顺便说一句,您的if(Boost_FOUND) 是不必要的,因为如果找不到 Boost,CMake 将失败。

    【讨论】:

    • Boost::log 添加到target_link_libraries 解决了这个问题。但是对于这个解决方案,有必要在两个地方指定 Boost 库,这是不好的。我检查了${Boost_LIBRARIES} 和库libboost_system-mt.alibboost_thread-mt.a 都在那里。
    • @Serbin 两个地方到底是什么意思?我只看到一个。 ${Boost_LIBRARIES} 不再需要了。
    • 我们应该在find_package和每个target_link_libraries中指定日志库。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多