Using ldd Command in Linux

Using ldd Command in Linux

The ldd is a command line tool that prints the shared library dependencies of an executable program or shared library. The ldd command can be useful when wanted to find missing dependencies.

This post presents usage examples of ldd command. Examples have been tested on Ubuntu 20.04 LTS.

1. Print shared library dependencies

Run ldd command and provide an executable program or shared library as argument to display shared library dependencies.

Print dependencies of executable program:

ldd /bin/grep

Output:

linux-vdso.so.1 (0x00007ffd9a372000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007fa65c056000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fa65c050000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fa65be5e000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fa65be3b000)
/lib64/ld-linux-x86-64.so.2 (0x00007fa65c105000)

Print dependencies of shared library:

ldd /lib/x86_64-linux-gnu/libpcre.so.3

Output:

linux-vdso.so.1 (0x00007ffd289e9000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f2c9ded0000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f2c9dcde000)
/lib64/ld-linux-x86-64.so.2 (0x00007f2c9df6e000)

2. Print shared library dependencies with details

The -v or --verbose option allows to display shared library dependencies with details, including symbol versioning information.

ldd -v /bin/grep

Output:

linux-vdso.so.1 (0x00007ffeea50b000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f3f79dcc000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f3f79dc6000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f3f79bd4000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f3f79bb1000)
/lib64/ld-linux-x86-64.so.2 (0x00007f3f79e7b000)

Version information:
/bin/grep:
        libc.so.6 (GLIBC_2.14) => /lib/x86_64-linux-gnu/libc.so.6
        libc.so.6 (GLIBC_2.4) => /lib/x86_64-linux-gnu/libc.so.6
        libc.so.6 (GLIBC_2.5) => /lib/x86_64-linux-gnu/libc.so.6
        libc.so.6 (GLIBC_2.3.4) => /lib/x86_64-linux-gnu/libc.so.6
        libc.so.6 (GLIBC_2.2.5) => /lib/x86_64-linux-gnu/libc.so.6
        libc.so.6 (GLIBC_2.3) => /lib/x86_64-linux-gnu/libc.so.6
/lib/x86_64-linux-gnu/libpcre.so.3:
        libpthread.so.0 (GLIBC_2.2.5) => /lib/x86_64-linux-gnu/libpthread.so.0
..........

3. Print unused direct dependencies

Use -u or --unused option to print unused direct dependencies. This means that shared library was listed as dependency when building binary but actually doesn't used in binary.

ldd -u /bin/grep

Output:

Unused direct dependencies:
        /lib/x86_64-linux-gnu/libdl.so.2

There are some notes for using ldd command.

1. Full path of executable program or shared library is required

The ldd command requires to provide the full path of executable program or shared library.

ldd wget

Output:

ldd: ./wget: No such file or directory

We can use whereis command to determine path of the binary, source, and manual page files.

whereis wget

Output:

wget: /usr/bin/wget /usr/share/man/man1/wget.1.gz /usr/share/info/wget.info.gz

Leave a Comment

Cancel reply

Your email address will not be published.