36

I have just built a shared lib on Ubuntu, and when I attempt to use the function, the application that loads the library is reporting 'xxx' symbol not found.

I want to check (i.e. list) the functions that are exported by my library so I can investigate this issue further.

Relevant details:

OS: Ubuntu 9.10 compiler: gcc 4.4.1 linker: GNU ld 2.20

Stick it to THE MAN
  • 5,621
  • 17
  • 77
  • 93

3 Answers3

29

Try the nm utility.

GNU nm lists the symbols from object files objfile.... If no object files are listed as arguments, nm assumes the file a.out. [reference]

rpg
  • 7,746
  • 3
  • 38
  • 43
  • 20
    **nm -C --defined-only -g libXXX.so** for C++ – Nikolai Fetissov Feb 16 '10 at 17:01
  • Thanks for that. It appears my symbol is exported, but undefined (it has a 'U' next to the function name). How can a function be exported and yet be undefined (IIRC, a linker option prevents this kind of absurbity). More importantly, what can I do to fix it? – Stick it to THE MAN Feb 16 '10 at 17:09
  • By "Fixing it", I mean how can I ensure that my functions are exported AND DEFINED in the shared library? – Stick it to THE MAN Feb 16 '10 at 17:10
  • Could it be that the undefined symbol is contained in another shared object? Check out Void's advice. – rpg Feb 16 '10 at 20:42
  • Were you able to find the root cause for your problem? I am trying to link a FORTRAN-based library and it is showing a function as undefined in the output shared library – user592748 Dec 29 '12 at 18:44
24
nm -D -C -g <library>

works well too.

Mark
  • 1,124
  • 1
  • 14
  • 21
  • 1
    For _exported_ functions add `--defined-only` option, otherwise this way you'll get imports too. Also, to get only _functions_ and not e.g. something like `_edata`, you should look for the symbols with `T` or `t` in the second column. So, something like this: `nm -DCg --defined-only $MY_LIB | grep '^[0-9a-f]\+ [Tt] '` – Ruslan Aug 29 '16 at 18:25
2

Is your shared library in the library load path or in the application's run-time search path? It sounds like the dynamic linker can't find your library. Try running ldd on your application to see if the library can be found at run-time, e.g.:

$ ldd /usr/bin/less
    linux-gate.so.1 =>  (0x0072a000)
    libncurses.so.5 => /lib/libncurses.so.5 (0x00c68000)
    libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0x007c7000)
    libdl.so.2 => /lib/tls/i686/cmov/libdl.so.2 (0x00286000)
    /lib/ld-linux.so.2 (0x002a1000)

See the ld.so(8) man page for additional details on library search paths.

Void - Othman
  • 3,441
  • 18
  • 18