g++ -Wl,--as-needed -pthread behaviour

Alan Modra amodra@gmail.com
Wed Sep 25 01:12:00 GMT 2013

On Tue, Sep 24, 2013 at 01:13:53PM +0100, Jonathan Wakely wrote:
> On 24 September 2013 02:22, Alan Modra wrote:
> >
> > Try compiling that testcase with -static rather than -Wl,--as-needed.
> > You'll hit std::system_error just like you do here.  I believe that is
> > a libstdc++ bug, and can be solved by making libstdc++.a use strong
> > references to pthread symbols from std::thread::join() (and perhaps
> > other objects that provide c++ thread support, if there are such).
> It's the std::thread constructor template that needs pthread_create.
> std::thread::join() needs pthread_join.

Ah, OK, I commented without looking at the source first, and it
shows.  :)

> > Otherwise we'd need to solve the transitive reference somehow.
> > ie. Teach the linker that a reference to std::thread::join() means
> > that pthread_create is required.  One obvious way to do that is have
> > the compiler reference pthread_create in objects that use
> > std::thread::join().
> How would we have the compiler do that?

The std::thread constructor needs to emit a strong reference to
pthread_create in the user object file.  Probably the best way to do
that is as Jakub said, inline it and modify __gthread_create to only
use weak references to pthread symbols when compiling for a shared
library.  (When I wrote the above comment I was thing more along the
lines of a dummy reference via a pointer variable initialised to
pthread_create, but inlining the lot is simpler.)

Alan Modra
Australia Development Lab, IBM

More information about the Gcc mailing list