C - 无法使用MSYS2和CMAKE链接到BOOST :: LOGGER

C++ - Cannot link to Boost::logger using msys2 and cmake

本文关键字:链接 CMAKE BOOST LOGGER MSYS2      更新时间:2023-10-16

从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]: *** [CMakeFilesProjectName.dirbuild.make:269: ProjectName.exe] Error 1
mingw32-make.exe[1]: *** [CMakeFilesMakefile2:67: CMakeFiles/ProjectName.dir/all] Error 2
mingw32-make.exe: *** [Makefile:94: all] Error 2

我了解,发生错误是因为链接器无法将libboost_log链接到Boost System库。它是否正确?但是系统库也应包含在${Boost_LIBRARIES}中。

如何解决此问题?

从cmake文件中可以看出,我将filesystem库用于其他内容,并且它正常工作。问题发生在我只包括日志库。

在我看来,您还必须添加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)

哪个自动设置所需的目录,以及2(链接到Boost::log的依赖项。

另外,作为附带说明,您的if(Boost_FOUND)是不必要的