【发布时间】:2019-09-05 00:20:17
【问题描述】:
在使用 pthread 库的程序上运行 make 时,我收到错误“未定义对 'pthread_create' 的引用”。当我直接用 g++ 构建它时它可以工作:
g++ -std=c++11 -pthread pthread_Mutex.c stopwatch.o -o pthread_Mutex
但不使用 CMake。
我研究了一些很好的例子,包括这里:cmake and 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 是我的主程序。 stopwatch.cpp 是一个不使用 pthread 的库。
但是在运行 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 文件中一定有问题,但我已经没有想法可以尝试了。
谢谢
【问题讨论】:
-
你在运行 CMake 后检查了
make VERBOSE=1的输出吗?这可以帮助验证g++获得的编译选项与没有 CMake 时获得的编译选项相同(当您直接运行g++时)。 -
我认为
# target_link_libraries(pthread_Mutex Threads::Threads)应该只适用于 3.1。看起来 cmake 没有为您设置正确的导入目标 Threads::Threads。您可以尝试 cmake --trace 查看详细的 cmake 内部日志以了解问题所在。 -
@squareskittles,我将 make VERBOSE=1 输出添加到我的原始帖子中。在我看来, -pthread 似乎被包括在内。我不清楚。
-
@2power10,我做了 cmake --trace。它用各种各样的东西填满了缓冲区。我应该寻找什么?