Linker trouble in GCC 4.8

Thought I’d share this one as it stumped me for a while. I discovered recently that Tarantula, a project I’ve been working on for YSTV, wouldn’t build using GCC 4.8 despite building fine with 4.7. It complained about not being able to resolve a few pthread symbols, specifically:

../../bin/ undefined reference to `pthread_create'
../../bin/ undefined reference to `pthread_detach'
../../bin/ undefined reference to `pthread_join'

It turns out in 4.8 the –as-needed linker flag is enabled by default, which will not link with symbols the linker doesn’t think are needed at build time. Unfortunately because of the way it works, if a library was specified in a link line before the file that depends on it, it will not be linked.

However, if instead a shared library has a dependency, it must be specified on that library’s link line rather than the main executable’s whereas it used to be enough to specify just for the executable. For me the solution was to add the $(LIBS) makefile var to the end of the link line for the .so file.

Alternatively, for the “big hammer” solution, the as-needed behaviour of GCC can be turned off using -Wl,–no-as-needed as a global compiler flag in the Makefile.

  1. –as-needed is a linker option, so nothing to do with your GCC version. Presumably you also changed linker, or upgraded your OS.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>