CMAKE_TRY_COMPILE_TARGET_TYPE set to STATIC_LIBRARY affects find_package(Threads) in Ubuntu 20.04
On Ubuntu 20.04 with GCC 9.4, the behavior of this code
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)`
changes when set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
appears before it.
Specifically, the behavior changes whether Performing Test CMAKE_HAVE_LIBC_PTHREAD
succeeds or fails.
With set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
first, Performing Test CMAKE_HAVE_LIBC_PTHREAD
succeeds, while without it, Performing Test CMAKE_HAVE_LIBC_PTHREAD
fails
This causes a library linking to Threads::Threads
to potentially link to pthreads or not just depending on the order, possibly removing required dependencies.
From reading the documentation, I believe that find_package(Threads REQUIRED)
uses try_compile()
under the hood, and so using the non-default STATIC_LIBRARY
setting for CMAKE_TRY_COMPILE_TARGET_TYPE
alters the behavior.
This discrepancy resulted in a PR to the Vulkan-Loader repo (of which I am its maintainer. https://github.com/KhronosGroup/Vulkan-Loader/pull/1431
Note that there is no ordering issue when compiling with Ubuntu 23.10 with GCC 13 - so this issue may be due to underlying problems with libc & pthread specific to Ubuntu 20.04.