C++ C++;-无法使用msys2和cmake链接到Boost::logger
构建Boost:logger教程中的简单示例:C++ C++;-无法使用msys2和cmake链接到Boost::logger,c++,logging,boost,cmake,C++,Logging,Boost,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) &
#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;
}
我收到下一条错误消息:
-- 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_日志链接到boost系统库。这是正确的吗?但系统库也应该包括在${Boost_库}
中
如何解决这个问题
从Cmake文件可以看出,我使用
文件系统
库来处理其他内容,并且它编译和工作正常。只有我包含日志库时才会出现此问题。在我看来,您还必须添加boost线程
库
无论如何,为了避免这些问题,我强烈建议使用目标语法来链接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)设置所需的include目录,2)链接到Boost::log
的依赖项
另外,作为旁白,您的
if(Boost\u FOUND)
是不必要的,因为如果没有找到Boost,CMake将失败。在我看来,您还必须添加Boost线程
库
无论如何,为了避免这些问题,我强烈建议使用目标语法来链接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)设置所需的include目录,2)链接到Boost::log
的依赖项
另外,作为旁白,您的
if(Boost\u FOUND)
是不必要的,因为如果没有找到Boost,CMake将失败。将Boost::log
添加到目标链接库中
解决了这个问题。但对于这个解决方案,有必要在两个地方指定Boost库,这是不好的。我用检查了${Boost_LIBRARIES}
和libboost_system-mt.a
,libboost_thread-mt.a显示在那里。@sebrin你说的两个地方到底是什么意思?我只看到一个${Boost\u LIBRARIES}
应该不再需要了。我们应该在find\u package
和每个target\u link\u LIBRARIES
中指定日志库。将Boost::Log
添加到target\u link\u LIBRARIES
解决了这个问题。但对于这个解决方案,有必要在两个地方指定Boost库,这是不好的。我用检查了${Boost_LIBRARIES}
和libboost_system-mt.a
,libboost_thread-mt.a显示在那里。@sebrin你说的两个地方到底是什么意思?我只看到一个<代码>${Boost\u LIBRARIES}应该不再需要了。我们应该在find\u package
和每个target\u link\u LIBRARIES
中指定日志库。