C ++链接错误(Boost线程)

时间:2015-03-31 12:50:15

标签: c++ boost cmake


我有一个boost线程库的问题。当我将这个库添加到我的项目时,它在编译时开始失败。我尝试添加到CXX_FLAGS -lboost_system -lboost_thread

的CMakeLists.txt:

cmake_minimum_required (VERSION 2.8)

set(PROJECT Refregiration_Telemetry-Device)

PROJECT(${PROJECT} CXX)

SET(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-std=c++11")

set(SRC_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/)
set(EXECUTABLE_OUTPUT_PATH ${SRC_ROOT_DIR}/bin)

set(SRC_DIR ${SRC_ROOT_DIR}/src)
set(INCLUDE_DIR ${SRC_ROOT_DIR}/include)
set(LIB_DIR ${SRC_ROOT_DIR}/lib)

INCLUDE_DIRECTORIES(${SRC_DIR} ${LIB_DIR} ${INCLUDE_DIR})

set(SOURCES main.cpp scheduler.cpp)
set(INCLUDES scheduler.hpp)

set(SOURCES_LIST)

foreach (class ${INCLUDES})
    LIST (APPEND SOURCES_LIST ${INCLUDE_DIR}/${class})
endforeach ()

foreach (class ${SOURCES})
    LIST (APPEND SOURCES_LIST ${SRC_DIR}/${class})
endforeach ()

#Boost include start
find_package (Boost COMPONENTS date_time thread)
include_directories (${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIR})
#set (ADDITIONAL_LIBS ${ADDITIONAL_LIBS} ${Boost_LIBRARIES})
#Boost include end

add_executable (${PROJECT} ${SOURCES_LIST})

target_link_libraries (${PROJECT} ${Boost_LIBRARIES})
#target_link_libraries (${PROJECT} ${ADDITIONAL_LIBS})

制作日志:

[ 50%] Building CXX object CMakeFiles/Refregiration_Telemetry-Device.dir/src/main.cpp.o
[100%] Building CXX object CMakeFiles/Refregiration_Telemetry-Device.dir/src/scheduler.cpp.o
Linking CXX executable ../bin/Refregiration_Telemetry-Device
/usr/bin/ld: CMakeFiles/Refregiration_Telemetry-Device.dir/src/main.cpp.o: undefined reference to symbol '_ZN5boost6system15system_categoryEv'
//usr/lib/x86_64-linux-gnu/libboost_system.so.1.54.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [../bin/Refregiration_Telemetry-Device] Error 1
make[1]: *** [CMakeFiles/Refregiration_Telemetry-Device.dir/all] Error 2
make: *** [all] Error 2

1 个答案:

答案 0 :(得分:1)

libboost_system.so是libboost_filesystem.so的主要要求,因此请确保将 -lboostsystem 添加到命令构建中,或将find_package(Boost 1.60.0 COMPONENTS filesystem system REQUIRED)添加到CMakeLists文件中。

PS:我使用 Boost 1.60.0 ,例如,您可以使用任何版本进行构建。此命令也适用于linux,用于在windows中构建,您需要使用visual studio developer命令提示符来构建boost。