I'm looking for the tool
/command
on Unix
platform to detect the library
dependencies of the .so
and .o
files.
I have already used the ldd
/nm
/truss
, but I don't know the proper approach to detect library dependencies.
It depends on what exactly is meant by "detect library dependencies".
The ldd
command works on shared libraries, not just on executables. It will display the dependencies of a shared library declared when the library was built:
$ ldd /usr/lib/libgtk-3.so
linux-vdso.so.1 (0x00007ffff8fff000)
libgdk-3.so.0 => /usr/lib/libgdk-3.so.0 (0x00007f43fcf47000)
libgmodule-2.0.so.0 => /usr/lib/libgmodule-2.0.so.0 (0x00007f43fcd43000)
libpangocairo-1.0.so.0 => /usr/lib/libpangocairo-1.0.so.0 (0x00007f43fcb36000)
libX11.so.6 => /usr/lib/libX11.so.6 (0x00007f43fc7fc000)
...
A library can have undefined symbols that are obtained by linking with further libraries not declared as dependencies. You can use objdump -T
or nm -D
to show the dynamic symbols - undefined symbols (those that should come from other libraries) will show up as *UND*
:
$ objdump -T /usr/lib/libgtk-3.so | head
/usr/lib/libgtk-3.so: file format elf64-x86-64
DYNAMIC SYMBOL TABLE:
0000000000066e38 l d .init 0000000000000000 .init
0000000000000000 DF *UND* 0000000000000000 g_param_spec_object
0000000000000000 DF *UND* 0000000000000000 g_utf8_validate
0000000000000000 DF *UND* 0000000000000000 g_date_get_month
0000000000000000 DF *UND* 0000000000000000 g_bookmark_file_get_visited
0000000000000000 DF *UND* 0000000000000000 g_value_get_float
From these symbol names it should be possible to deduce undeclared library dependencies.
Libraries that use pkg-config
or similar configuration mechanism sometimes fail to declare their dependencies at build-time, but declare the dependencies to pkg-config
, relying on the library users to use the tool to get the dependencies. pkg-config --libs
will list the dependencies in the format understood by the compiler:
$ pkg-config --libs gtk+-3.0
-lgtk-3 -lgdk-3 -latk-1.0 -lgio-2.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo-gobject -lpango-1.0 -lcairo -lgobject-2.0 -lglib-2.0