什么会导致以下问题: 我试图在 cmake 项目中使用 boost::log。
我的 CMakeList.txt 文件如下所示:
cmake_minimum_required(VERSION 3.10)
project(boostLogTest)
set(CMAKE_CXX_STANDARD 11)
add_executable(boostLogTest main.cpp)
ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK) ##### edit after DiCri's answer #####
find_package(Boost REQUIRED COMPONENTS log system)
if (Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
message("Boost VERSION: ${Boost_VERSION}")
message("Boost INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
message("Boost Boost_LIBRARY_DIRS: ${Boost_LIBRARY_DIRS}")
message("Boost LIBRARIES: ${Boost_LIBRARIES}")
TARGET_LINK_LIBRARIES(boostLogTest ${Boost_LIBRARIES})
endif ()
我的 main.cpp 文件如下所示:
#include <iostream>
#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 输出如下
-- Boost version: 1.64.0
-- Found the following Boost libraries:
-- log
-- system
-- date_time
-- log_setup
-- filesystem
-- thread
-- regex
-- chrono
-- atomic Boost VERSION: 106400 Boost INCLUDE_DIRS: /usr/local/include Boost Boost_LIBRARY_DIRS: /usr/local/lib Boost
LIBRARIES: /usr/local/lib/libboost_log.so;/usr/local/lib/libboost_system.so;/usr/local/lib/libboost_date_time.so;/usr/local/lib/libboost_log_setup.so;/usr/local/lib/libboost_filesystem.so;/usr/local/lib/libboost_thread.so;/usr/local/lib/libboost_regex.so;/usr/local/lib/libboost_chrono.so;/usr/local/lib/libboost_atomic.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/.../CLionProjects/boostLogTest/cmake-build-debug
但是在链接时我收到以下错误消息:
[ 50%] Linking CXX executable boostLogTest
CMakeFiles/boostLogTest.dir/main.cpp.o: In function `main':
/home/.../CLionProjects/boostLogTest/main.cpp:7: undefined reference to `boost::log::v2s_mt_posix::trivial::logger::get()'
/home/.../CLionProjects/boostLogTest/main.cpp:7: undefined reference to `boost::log::v2s_mt_posix::trivial::logger::get()'
/home/.../CLionProjects/boostLogTest/main.cpp:8: undefined reference to `boost::log::v2s_mt_posix::trivial::logger::get()'
/home/.../CLionProjects/boostLogTest/main.cpp:8: undefined reference to `boost::log::v2s_mt_posix::trivial::logger::get()'
/home/.../CLionProjects/boostLogTest/main.cpp:9: undefined reference to
boost::log::v2s_mt_posix::trivial::logger::get()' CMakeFiles/boostLogTest.dir/main.cpp.o:/home/.../CLionProjects/boostLogTest/main.cpp:9: more undefined references to
boost::log::v2s_mt_posix::trivial::logger::get()' follow ...
在 DiCri 的回答后编辑: 现在我收到以下错误消息:
[ 50%] Linking CXX executable boostLogTest
/usr/bin/ld: CMakeFiles/boostLogTest.dir/main.cpp.o: undefined reference to symbol 'pthread_rwlock_unlock@@GLIBC_2.2.5'
//lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
CMakeFiles/boostLogTest.dir/build.make:103: recipe for target 'boostLogTest' failed
make[3]: *** [boostLogTest] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/boostLogTest.dir/all' failed
make[2]: *** [CMakeFiles/boostLogTest.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/boostLogTest.dir/rule' failed
make[1]: *** [CMakeFiles/boostLogTest.dir/rule] Error 2
Makefile:118: recipe for target 'boostLogTest' failed
make: *** [boostLogTest] Error 2
如果我添加
set(Boost_USE_STATIC_LIBS ON)
库的路径更改为 *.a 文件,如:
Boost LIBRARIES: /usr/local/lib/libboost_log.a;/usr/local/lib/libboost_system.a;...
所有列出的库(*.so 和 *.a)都存在于此文件夹中。
我错过了什么?
最佳答案
只需尝试添加这一行
#define BOOST_LOG_DYN_LINK 1
作为 main.cpp 文件的第一行,我认为它应该可以工作,有人遇到了同样的问题 here
如果不行,尝试添加这一行
ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK)
到CMake文件 您还需要添加 OpenMP 包
关于c++ - 使用 cmake 错误链接 boost 日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51513653/