<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/87778>87778</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
libc++ does not link to pthread with GNU ld and older glibc
</td>
</tr>
<tr>
<th>Labels</th>
<td>
libc++
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
andreyv
</td>
</tr>
</table>
<pre>
Commit 36bb134ac79c91129d6ea551953ce67ed776123d removed the explicit linking of `pthread` and other libraries in libc++ on GNU/Linux. This is fine with glibc version 2.34 or later, as libpthread functionality [has been moved to libc](https://sourceware.org/git/?p=glibc.git;a=blob;f=NEWS;h=da4b2223e946368018dfb5ad73532ebf7dd84181;hb=HEAD#l1048). However, with older glibc versions libc++ now fails to link:
```
[ 98%] Linking CXX shared library llvm-18.1.3.build/lib/x86_64-unknown-linux-gnu/libc++.so
CMakeFiles/cxx_shared.dir/condition_variable.cpp.o: In function `std::__1::notify_all_at_thread_exit(std::__1::condition_variable&, std::__1::unique_lock<std::__1::mutex>)':
condition_variable.cpp:(.text._ZNSt3__125notify_all_at_thread_exitERNS_18condition_variableENS_11unique_lockINS_5mutexEEE+0x19): undefined reference to `pthread_getspecific'
condition_variable.cpp:(.text._ZNSt3__125notify_all_at_thread_exitERNS_18condition_variableENS_11unique_lockINS_5mutexEEE+0x3e): undefined reference to `pthread_setspecific'
condition_variable.cpp:(.text._ZNSt3__125notify_all_at_thread_exitERNS_18condition_variableENS_11unique_lockINS_5mutexEEE+0x4a): undefined reference to `pthread_getspecific'
```
libc++ sources have code such as
```cpp
#if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)
#pragma comment(lib, "pthread")
#endif
```
But this looks to be an LLVM extension and is not supported by the GNU linker, which is used by default when building libc++ on GNU/Linux.
Other libraries removed in the commit may have similar issues.
A workaround is to use `-DLIBCXX_ADDITIONAL_LIBRARIES=pthread`.
</pre>
<img width="1px" height="1px"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