简体   繁体   中英

C++ Linking error (Boost thread)


I've got a problem with boost thread library. When I add this library to my projects it starts to fail while compilation. I tried to add into CXX_FLAGS -lboost_system and -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})

Make log:

[ 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

libboost_system.so is the primary requirement of libboost_filesystem.so, so make sure add -lboostsystem to your command build or add find_package(Boost 1.60.0 COMPONENTS filesystem system REQUIRED) to CMakeLists file.

PS: I used Boost 1.60.0 for example, you can use whatever version for the build. Also this command is for linux, for building in windows you need to build boost using visual studio developer command prompt.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM