除非在 target_link_directories() 命令上调用 pthread,否则 CMake 构建将失败
CMake build fails unless pthread is called on the target_link_directories() command
我有一个链接问题,CMake 3.10
和Boost 1_66_0
。我正在使用asio异步计时器教程进行测试,因为我准备做一些网络。我正在从事一个需要我将 Boost 安装到自定义目录中的项目,正在进行最前沿的 Linux 缓存:
/home/myuser/boost/boost_1_66_0
我在.bash_profile
中设置了以下环境变量:
export BOOST_ROOT=/home/myuser/boost/boost_1_66_0
export BOOST_LIBRARYDIR=/home/myuser/boost/boost_1_66_0/stage/lib
尽管我设法使其正常工作,但除非在target_link_libraries()
命令上调用pthread
,否则构建会失败,即使我在find_package()
命令上调用 Boost 自己的thread
库。
我没有在 Boost 的入门指南或 CMake 的文档中找到任何提及需要调用pthread
。
这是我的完整CMakeLists.txt
文件:
1 cmake_minimum_required(VERSION 3.0)
2 project(asio_tut)
3 set(Boost_DEBUG ON)
4
5 if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
6 set(CMAKE_INSTALL_PREFIX=/home/myuser/projects/asio_tut/build CACHE PATH test FORCE)
7 endif(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
8
9 find_package(Boost REQUIRED COMPONENTS system thread)
10
11 if(Boost_FOUND)
12 include_directories(${Boost_INCLUDE_DIR})
13 add_executable(asio_tut timer_async.cpp)
14 target_link_libraries(asio_tut ${Boost_LIBRARIES})
15 endif()
CMake 找到thread
库:
-- [ /home/myuser/builds/cmake/share/cmake-3.10/Modules/FindBoost.cmake:1767 ] Boost_FOUND = 1
-- Boost version: 1.66.0
-- Found the following Boost libraries:
-- system
-- thread
-- chrono
-- date_time
-- atomic
-- Configuring done
-- Generating done
-- Build files have been written to: /home/myuser/projects/asio_tut/build
但随后它在继续make
命令上失败,因为它坚持pthread
:
[myuser@linux build]$ make
Scanning dependencies of target asio_tut
[ 50%] Building CXX object CMakeFiles/asio_tut.dir/timer_async.cpp.o
[100%] Linking CXX executable asio_tut
/usr/bin/ld: CMakeFiles/asio_tut.dir/timer_async.cpp.o: undefined reference to symbol 'pthread_condattr_setclock@@GLIBC_2.3.3'
/usr/lib/libpthread.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/asio_tut.dir/build.make:100: asio_tut] Error 1
make[1]: *** [CMakeFiles/Makefile2:68: CMakeFiles/asio_tut.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
要解决此问题,我必须向targe_link_libraries()
命令添加pthread
:
target_link_libraries(asio_tut ${Boost_LIBRARIES} pthread)
这正常吗?这以后会不会再来困扰我?它会导致可移植性出现任何问题吗?我应该忽略它吗?
我的CMakeCache.txt
文件显示 CMake 在自定义目录中找到了我所有的 Boost 库和标头。我不会包括整个缓存文件,但我检查了缓存条目,它们是正确的。
侧点
不确定这是否相关,但我确实在 CMake 构建期间收到了一个关于我的 Boost 版本的警告,因为它处于前沿:
CMake Warning at /home/myuser/builds/cmake/share/cmake-3.10/Modules/FindBoost.cmake:801 (message):
New Boost version may have incorrect or missing dependencies and imported
targets
我还认为CMake会使用Boost的线程库而不是要求pthread。
是的,Boost有自己的线程库(实际上是pthread的包装器)。要使用此库,您需要在找到 Boost 时请求相应的组件:
find_package(BOOST REQUIRED COMPONENTS system thread)
我前段时间也收到过这个错误。我刚刚将以下行添加到我的 CMakeList 中:
SET(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} )
SET(CMAKE_CXX_FLAGS " -pthread")
同样对于我的 3.0 CMakeList(也适用于更高版本),我添加了以下行:
if (CMAKE_VERSION VERSION_LESS "3.1")
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
endif ()
else ()
set_property(TARGET app_name PROPERTY CXX_STANDARD 11)
set_property(TARGET app_name PROPERTY CXX_STANDARD_REQUIRED ON)
endif ()
希望对您有所帮助。
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 函数调用中参数的顺序重要吗
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 除非在 target_link_directories() 命令上调用 pthread,否则 CMake 构建将失败
- C++:从 pthread 调用 sendmail 会导致管道中断
- Pthread 循环函数永远不会被调用
- 调用 gcc _without_ -pthread 的任何优点
- pthread:我想将 main 中的结构指针传递给 pthread 调用的函数
- 是否有任何 pthread 函数可以在最后一个线程终止时调用某些内容
- 从Windows窗体类调用PThread到Windows窗体类内的函数
- 多次调用多个pthread;比串行代码慢
- 在主函数中调用join子函数pthread
- pthread信令,无需内核调用
- 为每个pthread调用不同的函数
- 从 pthread调用的 OpenMP 代码的性能问题
- 从静态构造函数代码调用pthread库时失败