从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
库来处理其他东西,并且它已经编译并正常工作。出现此问题的原因是我只包含日志库。
答案 0 :(得分:2)
在我看来,您还必须添加boost thread
库。
无论如何,为了避免这些问题,我强烈建议使用目标语法来链接提升。
即,而不是
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)设置所需的包含目录,2)链接到Boost::log
的依赖项。
另外,作为旁注,您的if(Boost_FOUND)
是不必要的,因为如果找不到Boost,CMake将会失败。