除非在 target_link_directories() 命令上调用 pthread,否则 CMake 构建将失败

CMake build fails unless pthread is called on the target_link_directories() command

本文关键字:pthread 调用 否则 CMake 构建 失败 命令 target link directories      更新时间:2023-10-16

我有一个链接问题,CMake 3.10Boost 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 ()

希望对您有所帮助。