FindThreads fails if CMAKE_LINK_SEARCH_END_STATIC
CMakeLists.txt
cmake_minimum_required(VERSION 3.28)
project(test)
set(CMAKE_LINK_SEARCH_END_STATIC true)
find_package(Threads REQUIRED)
Output
root@8c04c339935a:/tmp/cmaketest/build# ~/cmake-3.28.0-rc2-linux-x86_64/bin/cmake ..
-- The C compiler identification is GNU 13.2.0
-- The CXX compiler identification is GNU 13.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
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - not found
-- Check if compiler accepts -pthread
-- Check if compiler accepts -pthread - no
CMake Error at /root/cmake-3.28.0-rc2-linux-x86_64/share/cmake-3.28/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
Could NOT find Threads (missing: Threads_FOUND)
Call Stack (most recent call first):
/root/cmake-3.28.0-rc2-linux-x86_64/share/cmake-3.28/Modules/FindPackageHandleStandardArgs.cmake:600 (_FPHSA_FAILURE_MESSAGE)
/root/cmake-3.28.0-rc2-linux-x86_64/share/cmake-3.28/Modules/FindThreads.cmake:226 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
CMakeLists.txt:5 (find_package)
It seemingly cannot find pthreads. The log reveals that it fails to build the test program for threads, complaining that /usr/bin/ld: cannot find -lgcc_s: No such file or directory
...
kind: "try_compile-v1"
backtrace:
- "/root/cmake-3.28.0-rc2-linux-x86_64/share/cmake-3.28/Modules/FindThreads.cmake:136 (try_compile)"
- "/root/cmake-3.28.0-rc2-linux-x86_64/share/cmake-3.28/Modules/FindThreads.cmake:179 (_threads_check_flag_pthread)"
- "CMakeLists.txt:5 (find_package)"
checks:
- "Check if compiler accepts -pthread"
directories:
source: "/tmp/cmaketest/build/CMakeFiles/CMakeScratch/TryCompile-r25b7F"
binary: "/tmp/cmaketest/build/CMakeFiles/CMakeScratch/TryCompile-r25b7F"
cmakeVariables:
CMAKE_C_FLAGS: ""
CMAKE_C_FLAGS_DEBUG: "-g"
CMAKE_EXE_LINKER_FLAGS: ""
CMAKE_LINK_SEARCH_END_STATIC: "true"
buildResult:
variable: "THREADS_HAVE_PTHREAD_ARG"
cached: true
stdout: |
Change Dir: '/tmp/cmaketest/build/CMakeFiles/CMakeScratch/TryCompile-r25b7F'
Run Build Command(s): /root/cmake-3.28.0-rc2-linux-x86_64/bin/cmake -E env VERBOSE=1 /usr/bin/gmake -f Makefile cmTC_32d2e/fast
/usr/bin/gmake -f CMakeFiles/cmTC_32d2e.dir/build.make CMakeFiles/cmTC_32d2e.dir/build
gmake[1]: Entering directory '/tmp/cmaketest/build/CMakeFiles/CMakeScratch/TryCompile-r25b7F'
Building C object CMakeFiles/cmTC_32d2e.dir/CheckForPthreads.c.o
/usr/bin/cc -o CMakeFiles/cmTC_32d2e.dir/CheckForPthreads.c.o -c /tmp/cmaketest/build/CMakeFiles/CMakeScratch/TryCompile-r25b7F/CheckForPthreads.c
Linking C executable cmTC_32d2e
/root/cmake-3.28.0-rc2-linux-x86_64/bin/cmake -E cmake_link_script CMakeFiles/cmTC_32d2e.dir/link.txt --verbose=1
/usr/bin/cc CMakeFiles/cmTC_32d2e.dir/CheckForPthreads.c.o -o cmTC_32d2e -pthread -Wl,-Bstatic
/usr/bin/ld: cannot find -lgcc_s: No such file or directory
/usr/bin/ld: cannot find -lgcc_s: No such file or directory
collect2: error: ld returned 1 exit status
gmake[1]: *** [CMakeFiles/cmTC_32d2e.dir/build.make:99: cmTC_32d2e] Error 1
gmake[1]: Leaving directory '/tmp/cmaketest/build/CMakeFiles/CMakeScratch/TryCompile-r25b7F'
gmake: *** [Makefile:127: cmTC_32d2e/fast] Error 2
exitCode: 2
The workaround is to just place the set(CMAKE_LINK_SEARCH_END_STATIC true)
after the find_package(Threads REQUIRED)
. Not a big issue, just thought I'd document it since I spent quite a while tracking it down and couldn't find any similar issues.