我与CMake 3.10
和Boost 1_66_0
存在关联问题。我准备做一些网络时,我正在使用asio async timer教程进行测试。我正在进行Linux工作,正在开发一个项目,需要我将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
虽然我设法让这个工作,但是构建失败,除非在pthread
命令上调用target_link_libraries()
,即使我在调用Boost自己的thread
库find_package()
命令。
我没有发现需要在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
答案 0 :(得分:6)
我还认为CMake会使用Boost的线程库,而不是要求pthread。
是的,Boost有自己的线程库(实际上是pthread的包装)。要使用此库,您需要在找到Boost:
时请求相应的组件find_package(BOOST REQUIRED COMPONENTS system thread)
答案 1 :(得分:1)
前一段时间我也收到了这个错误。我刚刚将这些行添加到我的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 ()
希望它会对你有所帮助。