具有Boost和CMake的单线程应用程序

时间:2015-07-26 19:29:49

标签: c++ boost cmake

我正在编写单线程应用程序。我用CMake编译它。我的应用程序是使用Boost用C ++编写的。运行'make'时,我的应用程序链接失败,并显示以下错误消息:

Linking CXX executable whisk
cd <project>/build/src && /usr/bin/cmake -E cmake_link_script CMakeFiles/whisk.dir/link.txt --verbose=1
/usr/bin/c++      CMakeFiles/whisk.dir/driver_simdag.cpp.o CMakeFiles/whisk.dir/whisk.cpp.o CMakeFiles/whisk.dir/driver_miror.cpp.o CMakeFiles/whisk.dir/driver.cpp.o CMakeFiles/whisk.dir/main.cpp.o  -o whisk -rdynamic -lboost_program_options 
/usr/bin/ld: CMakeFiles/whisk.dir/whisk.cpp.o: undefined reference to symbol 'pthread_rwlock_unlock@@GLIBC_2.2.5'
//lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

这是错误本身:

 undefined reference to symbol 'pthread_rwlock_unlock@@GLIBC_2.2.5'

来自src目录的我的CMakeLists.txt如下:

file(GLOB whisk_SRC "[a-zA-Z]*.cpp")
add_executable(whisk ${whisk_SRC})

FIND_PACKAGE( Boost 1.40 COMPONENTS program_options log REQUIRED )
set(Boost_USE_MULTITHREADED      OFF)
# set_property(TARGET whisk PROPERTY Boost_USE_MULTITHREADED OFF)
INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} )

set_property(TARGET whisk PROPERTY CXX_STANDARD 11)
set_property(TARGET whisk PROPERTY CXX_STANDARD_REQUIRED ON)

target_link_libraries(whisk ${Boost_PROGRAM_OPTIONS_LIBRARY})

install(TARGETS whisk DESTINATION bin)

我如何告诉CMake和Boost我根本没有pthreads,我想要编译单线程版本?

1 个答案:

答案 0 :(得分:1)

根据FindBoost文档页面,您需要使用

Boost_USE_MULTITHREADED - 设置为OFF以使用非多线程                            库('mt'标签)。默认为ON。

在致电find_package(Boost)之前设置此项。在my answerLinking boost library with Boost_USE_STATIC_LIB OFF on Windows中,我展示了如何使用cmake构建针对boost的详细信息。回答你问题的相关部分是

set(Boost_USE_MULTITHREADED OFF)
find_package( Boost 1.57.0 COMPONENTS system filesystem REQUIRED )

使用makefile时,我经常发现使用make VERBOSE=1来查看make系统正在调用的命令很有帮助。