Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cmake error undefined reference to `pthread_create'

Tags:

c++

linux

cmake

I make a test for cmake FindThreads. Here is my source code test.cpp and 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:

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. Thanks for any replying!

like image 414
Coco Avatar asked Aug 30 '25 17:08

Coco


1 Answers

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.

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). No reasons to worry and no reasons to look into CMakeError.txt.

did it really find Threads?

Yes, Threads are found. E.g., CMake clearly states

-- Found Threads: TRUE  

Other ways for deduce, that Threads has been found:

  1. You use REQUIRED keyword with find_package(Threads). Would Threads not found, CMake will report about an error and terminate configuration.

  2. You may check Threads_FOUND variable after the find_package(Threads) call. (With REQUIRED keyword this check is redudant).

  3. You may check Threads::Threads target after the find_package(Threads) call. (With REQUIRED keyword this check is redudant).

like image 62
Tsyvarev Avatar answered Sep 02 '25 15:09

Tsyvarev