# FindThreads module with static build?

**URL:** <https://discourse.cmake.org/t/findthreads-module-with-static-build/9846>\
**Category:** Code\
**Created:** [January 13, 2024, 7:33pm UTC](https://discourse.cmake.org/t/findthreads-module-with-static-build/9846 "2024-01-13T19:33:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anthonya](https://discourse.cmake.org/user_avatar/discourse.cmake.org/anthonya/32/4128_2.png) [@anthonya](https://discourse.cmake.org/u/anthonya)\
**Post date:** [January 13, 2024, 7:33pm UTC](https://discourse.cmake.org/t/findthreads-module-with-static-build/9846/1 "2024-01-13T19:33:26Z")

</div>

Hey there,

I was using the [FindThreads](https://cmake.org/cmake/help/latest/module/FindThreads.html) 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](https://stackoverflow.com/questions/58848694/gcc-whole-archive-recipe-for-static-linking-to-pthread-stopped-working-in-rec) to get right:

> New workaround: `-Wl,--whole-archive -lrt -lpthread -Wl,--no-whole-archive`

I attempted to use [CMAKE\__LINK\_LIBRARY\_USING_](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_LINK_LIBRARY_USING_FEATURE.html) to order to make this work, with the `WHOLE_ARCHIVE` [feature](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_LINK_LIBRARY_USING_FEATURE.html#predefined-features).

However, I get the following errors:

```auto
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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![anthonya](https://discourse.cmake.org/user_avatar/discourse.cmake.org/anthonya/32/4128_2.png) [@anthonya](https://discourse.cmake.org/u/anthonya)\
**Post date:** [January 13, 2024, 11:18pm UTC](https://discourse.cmake.org/t/findthreads-module-with-static-build/9846/2 "2024-01-13T23:18:11Z")

</div>

Just to update the thread here, I got something working:

```auto
# Required for glibc < 2.34
# https://discourse.cmake.org/t/findthreads-module-with-static-build/9846
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58909
if(BUILD_STATIC_LIBS AND NOT APPLE)
    target_link_options(Threads::Threads INTERFACE
            -pthread -Wl,--whole-archive -lrt -lpthread -Wl,--no-whole-archive)
endif()

```

Hopefully this unblocks others that hit the same issue.

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [January 16, 2024, 1:25pm UTC](https://discourse.cmake.org/t/findthreads-module-with-static-build/9846/3 "2024-01-16T13:25:51Z")

</div>

> [@anthonya](#):
>
> ` -pthread -Wl,--whole-archive -lrt -lpthread -Wl,--no-whole-archive)`

You probably want to tell CMake that there’s some structure here with the `whole-archive` bits. @marc.chevrier, can you verify that this works:

```cmake
-pthread $<LINK_LIBRARY:WHOLE_ARCHIVE,rt,pthread>

```

Needs CMake 3.24+ though.

---

<div class="post-metadata">

**Author:** ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)\
**Post date:** [January 16, 2024, 2:48pm UTC](https://discourse.cmake.org/t/findthreads-module-with-static-build/9846/4 "2024-01-16T14:48:34Z")

</div>

The syntax for `WHOLE_ARCHIVE` feature is correct, but the `$<LINK_LIBRARY>` genex is only valid to specify link libraries. So it is required to use `target_link_libraries()` command:

```cmake
if(BUILD_STATIC_LIBS AND NOT APPLE)
    target_link_options(Threads::Threads INTERFACE -pthread)
    target_link_libraries(Threads::Threads INTERFACE
            "$<LINK_LIBRARY:WHOLE_ARCHIVE,rt,pthread>")
endif()
```
