为什么 CMake 没有链接这个 CMakeList 中的 pthread.txt

Why is CMake not linking pthread in this CMakeList.txt?

本文关键字:CMakeList 中的 pthread txt CMake 链接 为什么      更新时间:2023-10-16

在使用 pthread 库的程序上运行 make 时出现错误"对'pthread_create'的未定义引用"。当我直接使用 g++ 构建它时,它可以工作:

g++ -std=c++11 -pthread pthread_Mutex.c stopwatch.o -o pthread_Mutex

但不是CMake。

我已经检查了一些很好的例子,包括这里:cmake和libpthread。

我已经尝试了 thehouse 的 3.1+ 和 2.8+ 版本的 CMake 示例。我在 Ubuntu 上运行 3.5.1。这是我的CMakeList.txt。

cmake_minimum_required(VERSION 3.5)
project(pthread_Mutex)
set (CMAKE_CXX_STANDARD 11)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
include_directories(include)
file(GLOB SOURCES "src/*.cpp") 
add_executable(stopwatch ${SOURCES})
add_executable(pthread_Mutex ${SOURCES})
# I've tried this one by itself 
#target_link_libraries(pthread_Mutex Threads::Threads)

if(THREADS_HAVE_PTHREAD_ARG)
  target_compile_options(pthread_Mutex PUBLIC "-pthread")
endif()
if(CMAKE_THREAD_LIBS_INIT)
  target_link_libraries(pthread_Mutex "${CMAKE_THREAD_LIBS_INIT}")
endif()
target_link_libraries(pthread_Mutex pthread) 

这是我的目录结构:

├── build
├── CMakeLists.txt
├── include
│   └── stopwatch.h
├── src
    ├── pthread_Mutex.cpp
    └── stopwatch.cpp

pthread_Mutex.cpp秒表.cpp是一个不使用 pthreads 的库。

但是我在运行 cmake VERBOSE=1 时出现以下错误:

/usr/bin/cmake -H/home/ben/workspaces/c++/thread_experiments -B/home/ben/workspaces/c++/thread_experiments/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/ben/workspaces/c++/thread_experiments/build/CMakeFiles /home/ben/workspaces/c++/thread_experiments/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/ben/workspaces/c++/thread_experiments/build'
make -f CMakeFiles/pthread_Mutex.dir/build.make CMakeFiles/pthread_Mutex.dir/depend
make[2]: Entering directory '/home/ben/workspaces/c++/thread_experiments/build'
cd /home/ben/workspaces/c++/thread_experiments/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/ben/workspaces/c++/thread_experiments /home/ben/workspaces/c++/thread_experiments /home/ben/workspaces/c++/thread_experiments/build /home/ben/workspaces/c++/thread_experiments/build /home/ben/workspaces/c++/thread_experiments/build/CMakeFiles/pthread_Mutex.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/home/ben/workspaces/c++/thread_experiments/build'
make -f CMakeFiles/pthread_Mutex.dir/build.make CMakeFiles/pthread_Mutex.dir/build
make[2]: Entering directory '/home/ben/workspaces/c++/thread_experiments/build'
make[2]: Nothing to be done for 'CMakeFiles/pthread_Mutex.dir/build'.
make[2]: Leaving directory '/home/ben/workspaces/c++/thread_experiments/build'
[ 50%] Built target pthread_Mutex
make -f CMakeFiles/stopwatch.dir/build.make CMakeFiles/stopwatch.dir/depend
make[2]: Entering directory '/home/ben/workspaces/c++/thread_experiments/build'
cd /home/ben/workspaces/c++/thread_experiments/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/ben/workspaces/c++/thread_experiments /home/ben/workspaces/c++/thread_experiments /home/ben/workspaces/c++/thread_experiments/build /home/ben/workspaces/c++/thread_experiments/build /home/ben/workspaces/c++/thread_experiments/build/CMakeFiles/stopwatch.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/home/ben/workspaces/c++/thread_experiments/build'
make -f CMakeFiles/stopwatch.dir/build.make CMakeFiles/stopwatch.dir/build
make[2]: Entering directory '/home/ben/workspaces/c++/thread_experiments/build'
[ 66%] Linking CXX executable stopwatch
/usr/bin/cmake -E cmake_link_script CMakeFiles/stopwatch.dir/link.txt --verbose=1
/usr/bin/c++      CMakeFiles/stopwatch.dir/src/pthread_Mutex.cpp.o CMakeFiles/stopwatch.dir/src/stopwatch.cpp.o  -o stopwatch 
CMakeFiles/stopwatch.dir/src/pthread_Mutex.cpp.o: In function `main':
pthread_Mutex.cpp:(.text+0x15a): undefined reference to `pthread_attr_getstacksize'
pthread_Mutex.cpp:(.text+0x19f): undefined reference to `pthread_attr_setstacksize'
pthread_Mutex.cpp:(.text+0x260): undefined reference to `pthread_create'
pthread_Mutex.cpp:(.text+0x29d): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
CMakeFiles/stopwatch.dir/build.make:120: recipe for target 'stopwatch' failed
make[2]: *** [stopwatch] Error 1
make[2]: Leaving directory '/home/ben/workspaces/c++/thread_experiments/build'
CMakeFiles/Makefile2:104: recipe for target 'CMakeFiles/stopwatch.dir/all' failed
make[1]: *** [CMakeFiles/stopwatch.dir/all] Error 2
make[1]: Leaving directory '/home/ben/workspaces/c++/thread_experiments/build'
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

所以我的 CMakeList.txt 文件中一定有问题,但我没有想法可以尝试。

谢谢

你已经使用了target_compile_options,这对于编译标志非常有用,但你忘了对链接选项做同样的事情。添加同一行,同时target_link_options

target_compile_options为编译步骤添加标志。您还需要链接到 pthread lib -

target_link_libraries(pthread_Mutex pthread)