Home > Back-end >  cmake stopped using -pthread
cmake stopped using -pthread

Time:02-18

After a system upgrade (Arch linux) I couldn't compile my project anymore. One of the problems was that there isn't a -pthread flag passed to the compiler anymore.

I managed to write a minimal test case:

The CMakeLists.txt file contains:

cmake_minimum_required(VERSION 3.22)
project(pthread_test)

set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)

add_executable(test test.cpp)
target_link_libraries(test Threads::Threads)

test.cpp in the same directory contains:

#ifndef _REENTRANT
#error "-pthread was not used"
#endif

int main()
{
}

Then running cmake as follows:

daniel:~/pthreadtest>cmake -DCMAKE_VERBOSE_MAKEFILE=ON .
-- The C compiler identification is GNU 11.2.0
-- The CXX compiler identification is GNU 11.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c   - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE  
-- Configuring done
-- Generating done
-- Build files have been written to: /home/carlo/pthreadtest

Results in a Makefile that does not use -pthread:

daniel:~/pthreadtest>make
/usr/bin/cmake -S/home/carlo/pthreadtest -B/home/carlo/pthreadtest --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/carlo/pthreadtest/CMakeFiles /home/carlo/pthreadtest//CMakeFiles/progress.marks
make  -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/carlo/pthreadtest'
make  -f CMakeFiles/test.dir/build.make CMakeFiles/test.dir/depend
make[2]: Entering directory '/home/carlo/pthreadtest'
cd /home/carlo/pthreadtest && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/carlo/pthreadtest /home/carlo/pthreadtest /home/carlo/pthreadtest /home/carlo/pthreadtest /home/carlo/pthreadtest/CMakeFiles/test.dir/DependInfo.cmake --color=
Consolidate compiler generated dependencies of target test
make[2]: Leaving directory '/home/carlo/pthreadtest'
make  -f CMakeFiles/test.dir/build.make CMakeFiles/test.dir/build
make[2]: Entering directory '/home/carlo/pthreadtest'
[ 50%] Building CXX object CMakeFiles/test.dir/test.cpp.o
/usr/bin/c      -MD -MT CMakeFiles/test.dir/test.cpp.o -MF CMakeFiles/test.dir/test.cpp.o.d -o CMakeFiles/test.dir/test.cpp.o -c /home/carlo/pthreadtest/test.cpp
/home/carlo/pthreadtest/test.cpp:2:2: error: #error "-pthread was not used"
    2 | #error "-pthread was not used"
      |  ^~~~~
make[2]: *** [CMakeFiles/test.dir/build.make:79: CMakeFiles/test.dir/test.cpp.o] Error 1
make[2]: Leaving directory '/home/carlo/pthreadtest'
make[1]: *** [CMakeFiles/Makefile2:86: CMakeFiles/test.dir/all] Error 2
make[1]: Leaving directory '/home/carlo/pthreadtest'
make: *** [Makefile:94: all] Error 2

Note that adding -pthread makes it work fine:

 daniel:~/pthreadtest>/usr/bin/c      -MD -MT CMakeFiles/test.dir/test.cpp.o -MF CMakeFiles/test.dir/test.cpp.o.d -o CMakeFiles/test.dir/test.cpp.o -c /home/carlo/pthreadtest/test.cpp -pthread

Can someone tell me what I am doing wrong? Or is this a bug in cmake?

CodePudding user response:

Your output of CMake misses a line like

Check if compiler accepts -pthread

which corresponds to flag THREADS_PREFER_PTHREAD_FLAG.

It seems that CMake 3.22 incorrectly ignores this flag when check CMAKE_HAVE_LIBC_PTHREAD is successful.

This should be fixed in CMake 3.23 (commit: https://github.com/Kitware/CMake/commit/68285bc8a91586ae47315679212eaef737af6cc0).

  • Related