How to make gcc or ld report undefined but won't let you down?

If you compile the shared library with GCC and pass the "-z defs" flag (which I think is just passed blindly to ld), then you get a good report of what symbols are undefined and ld fails (no .so file is created). On the other hand, if you do not specify "-z defs" or explicitly do not specify "-z nodefs" (the default), then .so will be released even if the symbols are missing, but you will not get a report on which symbols were missing. If there are any.

I would like both! I want the .so to be created, but I also need any missing characters. The only way I know to do this is to run it twice, once with "-z defs" and once without it. This means that the potentially long linking step is performed twice, which will further worsen the compilation / testing process.

If you are wondering about my ultimate goal - when compiling a library, the undefined symbols in the local object file indicate that the dependency is not specified, which should have been in my build environment, whereas if the symbol is missing from the library you are linking to, not error (the -l flags are only specified for direct dependencies, not dependency dependencies, on this system). I need a report for the part where it lists "links in file", so I can see if this symbol was referencing a local object or its associated library. The -allow-shlib- undefined option almost fixes this, but it doesn't work when linking against static libraries.

Preferred for solutions that will work with both GNU and Solaris linkers.

+2


source to share


2 answers


From GNU ld 2.15 NEWS file:



  • Improved linker handling of unresolved symbols. The - unresolved-symbols = switch has been added to tell the linker when it should report this, and the -warn-unresolved-symbol switch has been added to report as warning messages rather than errors.
+4


source


Instead of ld printing undefined characters during linking, you can use nm in the resulting .so file. For example:

nm --dynamic --undefined-only foo.so

      

EDIT: Although I think it doesn't give you the source files that use symbols. Sorry I missed this part of your question.



You can still use nm for an approximate solution along with grep:

for sym in `nm --dynamic --undefined-only foo.so |cut -d' ' -f11 |c++filt -p` ; do
    grep -o -e "\\<$sym\\>" *.cpp *.c *.h
done

      

This may have problems with local symbols with the same name, etc.

+6


source







All Articles