![](/img/trans.png)
[英]cmake returning error of “undefined reference to pthread_create”
[英]Cmake error undefined reference to `pthread_create'
我对 cmake FindThreads 进行了测试。 这是我的源代码 test.cpp 和 CMakeLists.txt:
#include <pthread.h>
void* test_func(void* data)
{
return data;
}
int main(void)
{
pthread_t thread;
pthread_create(&thread, NULL, test_func, NULL);
pthread_detach(thread);
pthread_cancel(thread);
pthread_join(thread, NULL);
pthread_atfork(NULL, NULL, NULL);
pthread_exit(NULL);
return 0;
}
cmake_minimum_required(VERSION 3.5)
project(test C CXX)
set(CMAKE_THREAD_PREFER_PTHREAD ON)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
add_executable(test test.cpp)
if(TARGET Threads::Threads)
target_link_libraries(test PRIVATE Threads::Threads)
endif()
当我跑步时:
cmake .
我得到输出:
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Check if compiler accepts -pthread
-- Check if compiler accepts -pthread - yes
-- Found Threads: TRUE
然后我检查 CMakeError.txt,发现:
gmake[1]: Entering directory '/home/hye/tmp/cmake-error/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_55ab6.dir/src.c.o
/usr/bin/clang -DCMAKE_HAVE_LIBC_PTHREAD -o CMakeFiles/cmTC_55ab6.dir/src.c.o -c /home/hye/tmp/cmake-error/CMakeFiles/CMakeTmp/src.c
Linking C executable cmTC_55ab6
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_55ab6.dir/link.txt --verbose=1
/usr/bin/clang -DCMAKE_HAVE_LIBC_PTHREAD CMakeFiles/cmTC_55ab6.dir/src.c.o -o cmTC_55ab6
/usr/bin/ld: CMakeFiles/cmTC_55ab6.dir/src.c.o: in function `main':
src.c:(.text+0x35): undefined reference to `pthread_create'
/usr/bin/ld: src.c:(.text+0x41): undefined reference to `pthread_detach'
/usr/bin/ld: src.c:(.text+0x4d): undefined reference to `pthread_cancel'
/usr/bin/ld: src.c:(.text+0x5f): undefined reference to `pthread_join'
clang-9: error: linker command failed with exit code 1 (use -v to see invocation)
gmake[1]: *** [CMakeFiles/cmTC_55ab6.dir/build.make:107: cmTC_55ab6] Error 1
gmake[1]: Leaving directory '/home/hye/tmp/cmake-error/CMakeFiles/CMakeTmp'
gmake: *** [Makefile:141: cmTC_55ab6/fast] Error 2
我的问题是为什么执行测试 CMAKE_HAVE_LIBC_PTHREAD - 失败了,既然失败了,它真的找到线程了吗,我完全困惑了。 感谢您的回复!
有一点理由检查CMakeError.txt
,直到 CMake 报告编译器不工作或 CMake 报告不可用功能,这是由探针编译/链接检测到的,但您希望该功能可用。
在您的情况下,您已成功配置 CMake(查看 CMake 输出中的最后几行),并且您已成功检测到与线程相关的库(见下文)。 没有理由担心,也没有理由查看CMakeError.txt
。
它真的找到线程了吗?
是的,找到了线程。 例如,CMake 明确指出
-- Found Threads: TRUE
其他推断方法,线程已被发现:
您将REQUIRED
关键字与find_package(Threads)
一起使用。 如果找不到线程,CMake 将报告错误并终止配置。
您可以在find_package(Threads)
调用后检查Threads_FOUND
变量。 (使用REQUIRED
关键字此检查是多余的)。
您可以在find_package(Threads)
调用后检查Threads::Threads
目标。 (使用REQUIRED
关键字此检查是多余的)。
FindThreads.cmake
会尽力确定编译器是否支持pthreads
作为库、编译时开关或链接时开关等。
您看到的故障是pthreads
库是否存在。 它没有。 相反,您应该使用-pthreads
编译文件,这是您的编译器接受的编译时开关。 这与clang
文档一致,该文档显示-pthreads
是编译器选项,而不是 linker 选项。 https://clang.llvm.org/docs/ClangCommandLineReference.html#compilation-flags
那么,它找到线程了吗? 不完全是,没有什么可找到的。 它确定了如何使用pthreads吗? 是的。
使用test.cpp
构建目标test
时,您没有显示任何错误。 当pthreads
未作为单独的库实现时,您只发布了人们预期的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.