Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tool for Library Dependency

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.

like image 522
Abinash Bishoyi Avatar asked Nov 21 '12 19:11

Abinash Bishoyi


People also ask

How do I find library dependencies?

Check Shared Library Dependencies of a Program Executable To find out what libraries a particular executable depends on, you can use ldd command. This command invokes dynamic linker to find out library dependencies of an executable.

What are library dependencies?

By definition, every shared library system provides a way for executables to depend on libraries, so that symbol resolution is deferred until runtime. An inter-library dependency is where a library depends on other libraries.

What tool can you use to print shared library dependencies?

Description. ldd prints the shared libraries required by each program or shared library specified on the command line.


1 Answers

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  
like image 60
user4815162342 Avatar answered Sep 28 '22 09:09

user4815162342