Reputation: 81
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!
Upvotes: 4
Views: 16538
Reputation: 65870
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:
You use REQUIRED
keyword with find_package(Threads)
. Would Threads not found, CMake will report about an error and terminate configuration.
You may check Threads_FOUND
variable after the find_package(Threads)
call. (With REQUIRED
keyword this check is redudant).
You may check Threads::Threads
target after the find_package(Threads)
call. (With REQUIRED
keyword this check is redudant).
Upvotes: 3
Reputation: 3554
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.
The failure you are seeing is if a pthreads
library exists. It doesn't. Instead you are supposed to compile the files with -pthreads
which is a compile time switch your compiler does accept. This is align with the clang
documentation which shows that -pthreads
is a compiler option, not a linker option. 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? Yes.
You didn't show any errors when building the target test
with test.cpp
. You only posted errors one would expect when pthreads
is not implemented as a separate library.
Upvotes: 0