2012-02-09

gcc 4.5.x or higher undefined reference link problem when shared library refers another shared library.


This is too much details of gcc, but, some developers might be interested in.

Recently I switched to gcc/g++ 4.6.x, then I experienced a linking problem of my C++ programs. It suddenly missing symbols. even some missing system symbols are reported  (dlopen, ostream operators...). For example,
libutil.so: undefined reference to `dlopen'
libutil.so: undefined reference to `dlclose'
libutil.so: undefined reference to `dlerror'
libutil.so: undefined reference to `dlsym'
I tried several things, checking libdl.so, manually add linker options.., but nothing helped. Finally I found http://wiki.debian.org/ToolChain/DSOLinking page.

gcc 4.5.x (or higher) passes --as-needed linker option by default, this gives you some missing symbol when your program linked shared library that implicitly linked shared library. For a package creation, this new default setting removes dependency, therefore, this default makes sense. However, this is a difficult problem.

Solving this problem, add --no-as-needed linker flag. E.g., '-Wl,--no-as-needed' in the compiler option.

No comments: