gpt4 book ai didi

c++ - 带有 boost 线程库的 Cmake

转载 作者:太空宇宙 更新时间:2023-11-04 12:35:30 25 4
gpt4 key购买 nike

我在链接 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'

缺少什么?

最佳答案

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

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

关于c++ - 带有 boost 线程库的 Cmake,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56575376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com