gpt4 book ai didi

c++ - 链接 Boost 线程库

转载 作者:行者123 更新时间:2023-11-30 01:08:48 26 4
gpt4 key购买 nike

我想在我的 C++ 项目中包含 Boost 的线程库。我的 CMake 文件是这样的:

cmake_minimum_required(VERSION 3.6)
project(LearningC)

find_package(Boost REQUIRED)
include_directories(${Boost_INCLUDE_DIR})

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES main.cpp Student.cpp Student.h)

add_executable(LearningC ${SOURCE_FILES})

target_link_libraries(LearningC ${Boost_LIBRARIES})

我得到一个错误:

Undefined symbols for architecture x86_64:
"boost::this_thread::interruption_point()", referenced from:
boost::condition_variable::wait(boost::unique_lock<boost::mutex>&) in main.cpp.o
[More stack traces...]

我做错了什么?

最佳答案

好的,我看到您找到了解决方案,但我想提出一些改进建议(只要您需要 CMake 3.6):

  • 使用导入的目标来管理每个目标的编译器/链接器选项,而不是“全局”变量和函数(如 include_directories(), ...)
  • 使用project()的完整签名定义一堆PROJECT_xxx变量,然后使用它们
  • 提供您使用的明确语言列表,以避免默认和可能的冗余检查
  • 在 Boost 的情况下,您不需要查找和链接隐式依赖项——FindBoost.cmake 会为您完成。仅指定您真正需要和使用的组件。

这是修改后的 CMakeLists.txt:

cmake_minimum_required(VERSION 3.6)
project(LearningCxx VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Boost COMPONENTS thread REQUIRED)

add_executable(
${PROJECT_NAME}
main.cpp
Student.cpp
)

target_link_libraries(
${PROJECT_NAME}
Boost::thread
)

更新:我已经为这个项目做了一个示例,但是删除了 Students.cpp 并将 main.cpp 替换为以下代码:

#include <boost/thread.hpp>
#include <iostream>

int main()
{
std::cout << boost::thread::hardware_concurrency() << std::endl;
}

这是我的测试运行:

zaufi@gentop〉…/tests/boost-thread-cmake/build〉empty dir〉cmake ..
-- The CXX compiler identification is GNU 5.4.0
-- Check for working CXX compiler: /usr/lib/outproc/bin/c++ -- works
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features - done
-- Looking for C++ include pthread.h - found
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Boost version: 1.62.0
-- Found the following Boost libraries:
-- thread
-- chrono
-- system
-- date_time
-- atomic
-- Configuring done
-- Generating done
-- Build files have been written to: /work/tests/boost-thread-cmake/build
zaufi@gentop〉…/tests/boost-thread-cmake/build〉default〉pfx: /usr/local〉make
Scanning dependencies of target LearningCxx
[ 50%] Building CXX object CMakeFiles/LearningCxx.dir/main.cpp.o
[100%] Linking CXX executable LearningCxx
[100%] Built target LearningCxx
zaufi@gentop〉…/tests/boost-thread-cmake/build〉default〉pfx: /usr/local〉ldd ./LearningCxx
linux-vdso.so.1 (0x00007fff73b75000)
libboost_thread.so.1.62.0 => /usr/lib64/libboost_thread.so.1.62.0 (0x00007fd1adafd000)
libboost_chrono.so.1.62.0 => /usr/lib64/libboost_chrono.so.1.62.0 (0x00007fd1ad8f6000)
libboost_system.so.1.62.0 => /usr/lib64/libboost_system.so.1.62.0 (0x00007fd1ad6f2000)
libboost_date_time.so.1.62.0 => /usr/lib64/libboost_date_time.so.1.62.0 (0x00007fd1ad4e1000)
libboost_atomic.so.1.62.0 => /usr/lib64/libboost_atomic.so.1.62.0 (0x00007fd1ad2df000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fd1ad0c3000)
libstdc++.so.6 => /usr/lib/gcc/x86_64-pc-linux-gnu/6.3.0/libstdc++.so.6 (0x00007fd1accc8000)
libm.so.6 => /lib64/libm.so.6 (0x00007fd1ac9cd000)
libgcc_s.so.1 => /usr/lib/gcc/x86_64-pc-linux-gnu/6.3.0/libgcc_s.so.1 (0x00007fd1ac7b6000)
libc.so.6 => /lib64/libc.so.6 (0x00007fd1ac41d000)
librt.so.1 => /lib64/librt.so.1 (0x00007fd1ac215000)
/lib64/ld-linux-x86-64.so.2 (0x00007fd1add25000)
zaufi@gentop〉…/tests/boost-thread-cmake/build〉default〉pfx: /usr/local〉cmake --version
cmake version 3.7.1

关于c++ - 链接 Boost 线程库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41393211/

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