我正在编写一个单线程应用程序。我使用 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 而我想要编译单线程版本?
最佳答案
根据FindBoost文档页面,你需要使用
Boost_USE_MULTITHREADED - 设置为 OFF 以使用非多线程 库('mt' 标签)。默认为开。
在调用 find_package(Boost)
之前设置它。在 my answer至 Linking 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 系统正在调用的命令。
关于c++ - 使用 Boost 和 CMake 的单线程应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31641063/