C - 无法使用MSYS2和CMAKE链接到BOOST :: LOGGER
C++ - Cannot link to Boost::logger using msys2 and cmake
从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)
是不必要的
相关文章:
- CMake-按正确顺序将项目与C运行时对象文件链接
- 无法链接 CMake 中的本地库
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- Cmake 链接问题:未定义对 Button::mousePressEvent(QGraphicsSceneMouseE
- 在 CMake 中为每个目标设置编译器/链接器标志
- 链接 cmake 时出现未定义的引用错误
- 如何将GTest与CMake一起使用?遵循谷歌指南时的链接问题
- 将 SDL2 与 CMake 链接起来
- CMake 库链接使用两个不同的编译器版本
- CMake 无法链接外部库
- CMake - 更改共享库链接选项
- CMake 3.5 中的链接器标志位置
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 如何避免在 cmake 中自动链接 Qt5 库?
- 将源中的 GLEW 与 cmake 静态链接
- CMake 错误:链接器命令失败,退出代码为 1 和 cpp.o 文件
- 找不到 CMake 链接的库
- Opencv 链接 CMAKE 不起作用 Ubuntu 16.04 Docker
- 如何在没有外部脚本的情况下链接CMake和SQLite
- 链接CMake的Openscenegraph库