nm is a very useful command for programmers. It can be used to list the symbols defined in an executable, object file (
.o) or shared object library file (
I typically need this command when the compiler throws me a undefined reference error. By searching for the reference symbol among the symbols exported by a library file, I can figure out if that is the one that needs to be linked into the target executable.
- To list all symbols exported by an object file, use the
$ nm -g foo.so
Other options that I find useful:
-C: This option demangles symbol names to the names used in C or C++ code. By default, symbol names are mangled. The C symbols have extra underscores and the C++ symbols are completely mangled to encode the namespace, class and type information.
-A: Prepend each line with path of object file. This is useful when you pipe this output through other programs, like grep.
-D: Display dynamic symbols, rather than normal symbols. By default, the symbols in the
.strtabsections are shown. By specifying this option, the symbols in
.dynstrsections are shown.
--defined-only: Use this option to get only the symbols defined in the object file. By default, all the symbols in the object file are shown. I typically use this when I am solving an undefined reference problem.
Tried with: Binutils 2.24 and Ubuntu 14.04