cmake 停止使用 -pthread

cmake stopped using -pthread

系统升级后(Arch linux)我无法再编译我的项目了。 问题之一是不再有 -pthread 标志传递给编译器。

我设法写了一个最小的测试用例:

CMakeLists.txt 文件包含:

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 在同一目录包含:

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

int main()
{
}

然后运行cmake如下:

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

生成不使用 -pthread 的 Makefile:

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

请注意,添加 -pthread 可以正常工作:

 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

有人可以告诉我我做错了什么吗?或者这是 cmake 中的错误?

您的 CMake 输出漏掉了一行

Check if compiler accepts -pthread

对应标志 THREADS_PREFER_PTHREAD_FLAG.

似乎 CMake 3.22 在检查 CMAKE_HAVE_LIBC_PTHREAD 成功时错误地忽略了这个标志。

这应该在 CMake 3.23 中修复(提交:https://github.com/Kitware/CMake/commit/68285bc8a91586ae47315679212eaef737af6cc0)。