Hey there,
I was using the FindThreads module in my project, and all is fine when building a dynamic library/executable.
The issue comes in when building a fully static binary. If using glibc < 2.34, the thread library has some issues (due to weak symbols) and requires some adjusting to get right:
New workaround:
-Wl,--whole-archive -lrt -lpthread -Wl,--no-whole-archive
I attempted to use CMAKE_LINK_LIBRARY_USING to order to make this work, with the WHOLE_ARCHIVE
feature.
However, I get the following errors:
CMake Warning (dev) at src/CMakeLists.txt:22 (add_library):
The feature 'WHOLE_ARCHIVE', specified as part of a generator-expression
'$<LINK_LIBRARY:WHOLE_ARCHIVE>', will not be applied to the INTERFACE
library 'Threads::Threads'.
This warning is for project developers. Use -Wno-dev to suppress it.
CMake Warning (dev) at tests/CMakeLists.txt:12 (add_executable):
The feature 'WHOLE_ARCHIVE', specified as part of a generator-expression
'$<LINK_LIBRARY:WHOLE_ARCHIVE>', will not be applied to the INTERFACE
library 'Threads::Threads'.
This warning is for project developers. Use -Wno-dev to suppress it.
I then tried the following:
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
set(THREAD_LIBRARY Threads::Threads)
if(BUILD_STATIC_LIBS AND NOT APPLE)
set(THREAD_LIBRARY -Wl,--whole-archive -lrt -lpthread -Wl,--no-whole-archive)
endif()
But CMake somehow hijacks the -lpthread, as the resultant build command gets modified:
... -Wl,--whole-archive -lrt -Wl,--no-whole-archive ... -pthread ...
What is the expected way to achieve this on the latest version of CMake?