tags:

views:

84

answers:

1

Is there a built in linux utility that I can use to test a newly compiled shared library for external undefined references? Gcc seems to be intelligent enough to check for undefined symbols in my own binary, but if the symbol is a reference to another library gcc does not check at link time. Instead I only get the message when I try to link to my new library from another program.

It seems a little silly to get undefined reference messages in a library when I am compiling a different project so I want to know if I can do a check on all references internal and external when I build the library not when I link to it.

Example error:

make -C  UnitTests debug
make[1]: Entering directory `~/projects/Foo/UnitTests`
g++ [ tons of objects ] -L../libbar/bin -lbar -o UnitTests
libbar.so: undefined reference to `DoSomethingFromAnotherLibrary`
collect2: ld returned 1 exit status
make[1]: *** [~/projects/Foo/UnitTests] Error 1
+2  A: 

Usually, undefined references are allowed when linking shared objects, but you can make the linker generate an error if there are undefined symbols in the object files that you are linking to create the shared library by supplying -z defs to the linker (or equivalently -Wl,-z,defs in the gcc command that calls the linker).

Charles Bailey
haha, brilliant. Now I can tell who is calling the undefined function when compiling the library! MUCH better than a generic message when linking to the library. Cheers! I am kind of curious why ld allows unresolved externals in shared libraries now..
Charles
@Charles: It's possible (if unusual) to have shared object symbols resolved by symbols in the object files in the executable so `-z defs` might not always be what you want. It's also possible the version of the library used at final link time might not be the version used at final link or load time so the final link might work even if `-z defs` would have generated an error earlier on in the build process. In most of the situations where I've created shared objects, `-z defs` works and is useful though.
Charles Bailey