简体   繁体   English

Cmake 错误未定义对“pthread_create”的引用

[英]Cmake error undefined reference to `pthread_create'

I make a test for cmake FindThreads.我对 cmake FindThreads 进行了测试。 Here is my source code test.cpp and CMakeLists.txt:这是我的源代码 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()

when I run:当我跑步时:

cmake .

I get the outpu:我得到输出:

-- 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  

then I check CMakeError.txt, find that:然后我检查 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         

My question is why performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed, and since it failed, did it really find Threads, I am totally confused.我的问题是为什么执行测试 CMAKE_HAVE_LIBC_PTHREAD - 失败了,既然失败了,它真的找到线程了吗,我完全困惑了。 Thanks for any replying!感谢您的回复!

There is a little reason to examine CMakeError.txt until CMake reports about not-working compiler or CMake reports about unavailable feature, which is detected by probe compilation/linking, but you expect that feature to be available.有一点理由检查CMakeError.txt ,直到 CMake 报告编译器不工作或 CMake 报告不可用功能,这是由探针编译/链接检测到的,但您希望该功能可用。

In your case you have successful CMake configuration (look at the last lines in CMake output), and you have successfully detected Threads-related library(see below).在您的情况下,您已成功配置 CMake(查看 CMake 输出中的最后几行),并且您已成功检测到与线程相关的库(见下文)。 No reasons to worry and no reasons to look into CMakeError.txt .没有理由担心,也没有理由查看CMakeError.txt

did it really find Threads?它真的找到线程了吗?

Yes, Threads are found .是的,找到了线程。 Eg, CMake clearly states例如,CMake 明确指出

-- Found Threads: TRUE  

Other ways for deduce, that Threads has been found:其他推断方法,线程已被发现:

  1. You use REQUIRED keyword with find_package(Threads) .您将REQUIRED关键字与find_package(Threads)一起使用。 Would Threads not found, CMake will report about an error and terminate configuration.如果找不到线程,CMake 将报告错误并终止配置。

  2. You may check Threads_FOUND variable after the find_package(Threads) call.您可以在find_package(Threads)调用后检查Threads_FOUND变量。 (With REQUIRED keyword this check is redudant). (使用REQUIRED关键字此检查是多余的)。

  3. You may check Threads::Threads target after the find_package(Threads) call.您可以在find_package(Threads)调用后检查Threads::Threads目标。 (With REQUIRED keyword this check is redudant). (使用REQUIRED关键字此检查是多余的)。

FindThreads.cmake tries it best to determine if the compiler supports pthreads as a library, a compile time switch, or a link time switch, etcetera. FindThreads.cmake会尽力确定编译器是否支持pthreads作为库、编译时开关或链接时开关等。

The failure you are seeing is if a pthreads library exists.您看到的故障是pthreads库是否存在。 It doesn't.它没有。 Instead you are supposed to compile the files with -pthreads which is a compile time switch your compiler does accept.相反,您应该使用-pthreads编译文件,这是您的编译器接受的编译时开关。 This is align with the clang documentation which shows that -pthreads is a compiler option, not a linker option.这与clang文档一致,该文档显示-pthreads是编译器选项,而不是 linker 选项。 https://clang.llvm.org/docs/ClangCommandLineReference.html#compilation-flags https://clang.llvm.org/docs/ClangCommandLineReference.html#compilation-flags

So, did it find Threads?那么,它找到线程了吗? Not exactly, there is nothing to find.不完全是,没有什么可找到的。 Did it determine how to use pthreads?它确定了如何使用pthreads吗? Yes.是的。

You didn't show any errors when building the target test with test.cpp .使用test.cpp构建目标test时,您没有显示任何错误。 You only posted errors one would expect when pthreads is not implemented as a separate library.pthreads未作为单独的库实现时,您只发布了人们预期的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM