【发布时间】: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 库来处理其他东西,并且它编译并正常工作。只有我包含日志库才会出现此问题。
【问题讨论】: