【发布时间】:2018-01-13 05:33:22
【问题描述】:
我遇到了与 CMake 3.10 和 Boost 1_66_0 的链接问题。当我准备做一些网络时,我正在使用asio async timer 教程进行测试。我正在处理一个需要我将 Boost 安装到自定义目录的项目:
/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 的 getting started guide 或 CMake 的 documentation 中,我没有发现需要调用 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
要解决此问题,我必须将 pthread 添加到 targe_link_libraries() 命令:
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
【问题讨论】:
-
您是否尝试过将
threads添加为要求?find_package(Threads REQUIRED). -
请注意,任何已发布的 CMake 版本都不支持您的 Boost 版本。这可能会导致细微的错误。不确定您是否受到影响。 stackoverflow.com/a/42124857/2799037
-
有趣。我确实收到了一个关于我的 Boost 版本的警告,因为它是最前沿的。我将其添加到我的问题中。