Tag Archives: undefined reference

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/libCaspar.so: undefined reference to `pthread_create'
../../bin/libCaspar.so: undefined reference to `pthread_detach'
../../bin/libCaspar.so: 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.