具有Boost线程库的CMake

时间:2019-06-13 07:27:42

标签: c++ cmake boost-asio

我在链接boost库时遇到了一些问题, 这是我要构建的项目结构:

/my_project
 --> CMakeLists.txt
 --> main.cpp
 --> /Serial
      --> CMakeLists.txt
      --> src
           --> Serial.cpp
      --> inc
            --> Serial.h

Serial / CMakeLists.txt:

## Target
add_library(Serial src/Serial.cpp inc/Serial.h)

## Link libraries
set(BOOST_LIBS date_time system)
find_package(Boost COMPONENTS ${BOOST_LIBS} REQUIRED)
target_link_libraries(Serial ${Boost_LIBRARIES})
find_package(Threads REQUIRED)
target_link_libraries(Serial ${CMAKE_THREAD_LIBS_INIT})
target_include_directories(Serial PUBLIC
   $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/inc>)

项目CMakeLists.txt:

cmake_minimum_required(VERSION 3.1)
project(TEST)

## Target
set(CMAKE_CXX_STANDARD 11)
set(TEST_SRCS main.cpp)
add_executable(simple ${TEST_SRCS})
add_subdirectory(Serial)


## Link libraries
set(BOOST_LIBS date_time system)
find_package(Boost COMPONENTS ${BOOST_LIBS} REQUIRED)
target_link_libraries(simple ${Boost_LIBRARIES})
target_link_libraries(simple Serial)

但是当我尝试构建程序时,这是显示以下错误:

Serial/libSerial.a(Serial.cpp.o): In function   `boost::detail::thread_data_base::thread_data_base()':
Serial.cpp:   (.text._ZN5boost6detail16thread_data_baseC2Ev[_ZN5boost6detail16thread_da ta_baseC5Ev]+0x20): undefined reference to `vtable for boost::detail::thread_data_base'
Serial/libSerial.a(Serial.cpp.o): In function `boost::detail::interruption_checker::interruption_checker(pthread_mutex_t*, pthread_cond_t*)':
Serial.cpp:(.text._ZN5boost6detail20interruption_checkerC2EP15pthread_mutex_tP14pthread_cond_t[_ZN5boost6detail20interruption_checkerC5EP15pthread_mutex_tP14pthread_cond_t]+0x25): undefined reference to `boost::detail::get_current_thread_data()'
 Serial/libSerial.a(Serial.cpp.o): In function `boost::thread::start_thread()':
 Serial.cpp:(.text._ZN5boost6thread12start_threadEv[_ZN5boost6thread12start_threadEv]+0x24): undefined reference to `boost::thread::start_thread_noexcept()'
Serial/libSerial.a(Serial.cpp.o): In function `boost::thread::~thread()':
Serial.cpp:(.text._ZN5boost6threadD2Ev[_ZN5boost6threadD5Ev]+0x14): undefined reference to `boost::thread::detach()'
Serial/libSerial.a(Serial.cpp.o): In function `boost::thread::get_id() const':
Serial.cpp:(.text._ZNK5boost6thread6get_idEv[_ZNK5boost6thread6get_idEv]+0x18): undefined reference to `boost::thread::native_handle()'
Serial/libSerial.a(Serial.cpp.o): In function `boost::thread::join()':
Serial.cpp:(.text._ZN5boost6thread4joinEv[_ZN5boost6thread4joinEv]+0x7e): undefined reference to `boost::thread::join_noexcept()'
 Serial/libSerial.a(Serial.cpp.o): In function `boost::condition_variable::wait(boost::unique_lock<boost::mutex>&)':
 Serial.cpp:(.text._ZN5boost18condition_variable4waitERNS_11unique_lockINS_5mutexEEE[_ZN5boost18condition_variable4waitERNS_11unique_lockINS_5mutexEEE]+0xe4): undefined reference to `boost::this_thread::interruption_point()'
Serial/libSerial.a(Serial.cpp.o): In function `SerialImpl::SerialImpl()':
Serial.cpp:(.text._ZN10SerialImplC2Ev[_ZN10SerialImplC5Ev]+0x3c): undefined reference to `boost::thread::thread()'
Serial/libSerial.a(Serial.cpp.o): In function `boost::detail::thread_data<boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> > > >::~thread_data()':
Serial.cpp:(.text._ZN5boost6detail11thread_dataINS_3_bi6bind_tImNS_4_mfi3mf0ImNS_4asio10io_serviceEEENS2_5list1INS2_5valueIPS7_EEEEEEED2Ev[_ZN5boost6detail11thread_dataINS_3_bi6bind_tImNS_4_mfi3mf0ImNS_4asio10io_serviceEEENS2_5list1INS2_5valueIPS7_EEEEEEED5Ev]+0x22): undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
Serial/libSerial.a(Serial.cpp.o):(.data.rel.ro._ZTIN5boost6detail11thread_dataINS_3_bi6bind_tImNS_4_mfi3mf0ImNS_4asio10io_serviceEEENS2_5list1INS2_5valueIPS7_EEEEEEEE[_ZTIN5boost6detail11thread_dataINS_3_bi6bind_tImNS_4_mfi3mf0ImNS_4asio10io_serviceEEENS2_5list1INS2_5valueIPS7_EEEEEEEE]+0x10): undefined reference to `typeinfo for boost::detail::thread_data_base'

缺少什么?

1 个答案:

答案 0 :(得分:0)

我认为我对这些库感到困惑,解决方案是在Serial / CMakeLists.txt中添加:

find_package(Boost REQUIRED COMPONENTS thread)
target_link_libraries(Serial Boost::thread)