https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127158

            Bug ID: 127158
           Summary: Missing -lpthread for libstdc++ for older glibc
           Product: gcc
           Version: 15.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ossman at cendio dot se
  Target Milestone: ---

When targeting older (but still in use and supported) versions of glibc, g++
fails to link code using std::thread. The issue is that -lpthread is never
added, despite libstdc++ introducing dependencies through the templates.

Example error:

> /usr/bin/ld: ../../common/rfb/librfbclient.a(DecodeManager.cxx.o): undefined 
> reference to symbol 'pthread_join@@GLIBC_2.2.5'

There are no direct calls to pthread_join() in the application code, only to
std::thread::join():

>     thread->join();


This is for dynamically linked libstdc++. With -static-libstdc++, the situation
is even worse:

> /usr/bin/ld: 
> /usr/lib/gcc/x86_64-unknown-linux-gnu/15.2.0/../../../../lib64/libstdc++.a(thread.o):
>  in function `__gthread_join(unsigned long, void**)':
> /opt/docker-cenbuild/cenbuild/repo/rpmbuild/BUILD/gcc-15.2.0/build/x86_64-unknown-linux-gnu/libstdc++-v3/include/x86_64-unknown-linux-gnu/bits/gthr-default.h:715:(.text._ZNSt6thread4joinEv+0x17):
>  undefined reference to `pthread_join'
> /usr/bin/ld: 
> /usr/lib/gcc/x86_64-unknown-linux-gnu/15.2.0/../../../../lib64/libstdc++.a(thread.o):
>  in function `__gthread_create(unsigned long*, void* (*)(void*), void*)':
> /opt/docker-cenbuild/cenbuild/repo/rpmbuild/BUILD/gcc-15.2.0/build/x86_64-unknown-linux-gnu/libstdc++-v3/include/x86_64-unknown-linux-gnu/bits/gthr-default.h:709:(.text._ZNSt6thread15_M_start_threadESt10unique_ptrINS_6_StateESt14default_deleteIS1_EEPFvvE+0x29):
>  undefined reference to `pthread_create'
> /usr/bin/ld: 
> /opt/docker-cenbuild/cenbuild/repo/rpmbuild/BUILD/gcc-15.2.0/build/x86_64-unknown-linux-gnu/libstdc++-v3/include/x86_64-unknown-linux-gnu/bits/gthr-default.h:709:(.text._ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEEPFvvE+0x6c):
>  undefined reference to `pthread_create'
> /usr/bin/ld: ../../common/rfb/librfbclient.a(DecodeManager.cxx.o): in 
> function > `std::thread::_M_thread_deps_never_run()':
> /usr/include/c++/15.2.0/bits/std_thread.h:156:(.text._ZNSt6thread24_M_thread_deps_never_runEv[_ZNSt6thread24_M_thread_deps_never_runEv]+0x5):
>  undefined reference to `pthread_create'
> /usr/bin/ld: 
> /usr/include/c++/15.2.0/bits/std_thread.h:157:(.text._ZNSt6thread24_M_thread_deps_never_runEv[_ZNSt6thread24_M_thread_deps_never_runEv]+0xa):
>  undefined reference to `pthread_join'

Applications can work around this by explicitly adding -lpthread themselves.
But it's a bit silly requirement when the application code never even includes
pthread.h.


(This is of course not an issue with modern versions of glibc, which no longer
has pthread symbols in a separate library)

Reply via email to