链接Boost线程库

时间:2016-12-30 08:43:50

标签: c++ boost cmake

我试图在我的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...]

我做错了什么?

2 个答案:

答案 0 :(得分:3)

好的,我发现你找到了一个解决方案,但我想提出一些改进建议(一旦你需要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

答案 1 :(得分:0)

我找到了解决方案。基本上,Boost的大部分代码都是用C ++头文件(.hpp)编写的。但是有些库需要编译和链接......下面的代码可以工作!

cmake_minimum_required(VERSION 3.6)
project(LearningC)

set(CMAKE_CXX_STANDARD 11)

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

add_executable(LearningC ${SOURCE_FILES})

find_package(Boost COMPONENTS thread system REQUIRED)

include_directories(${Boost_INCLUDE_DIR})
target_link_libraries(LearningC ${Boost_LIBRARIES})