"ldd" - Dynamic Library Dependency Checker

This section provides a tutorial example on how to use the 'ldd' tool to list dynamic library dependencies of any executable files, including dynamic library files on Linux systems.

Most of executable programs use dynamic libraries because of two main benefits:

If you want to know which dynamic libraries are used by an executable program, you can use the "ldd" command on Linux systems. Here are some usage examples of the "ldd" command.

1. List dynamic library dependency of the "ls" system command program. I see 8 system libraries.

herong$ which ls
  /usr/bin/ls

herong$ ldd /bin/ls
  linux-vdso.so.1 (0x00007ffd03779000)
  libselinux.so.1 => /lib64/libselinux.so.1 (0x00007fe8a18cd000)
  libcap.so.2 => /lib64/libcap.so.2 (0x00007fe8a16c7000)
  libc.so.6 => /lib64/libc.so.6 (0x00007fe8a1304000)
  libpcre2-8.so.0 => /lib64/libpcre2-8.so.0 (0x00007fe8a1080000)
  libdl.so.2 => /lib64/libdl.so.2 (0x00007fe8a0e7c000)
  /lib64/ld-linux-x86-64.so.2 (0x00007fe8a1d1b000)
  libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fe8a0c5c000)

2. List dynamic library dependency of my own C program compiled from hello.c. I see 2 Linux system libraries and 1 C library.

herong$ gcc -o hello hello.c

herong$ ./hello
Hello world! - From C.

herong$ ldd hello
  linux-vdso.so.1 (0x00007ffed5fc6000)
  libc.so.6 => /lib64/libc.so.6 (0x00007f3f1f26f000)
  /lib64/ld-linux-x86-64.so.2 (0x00007f3f1f632000)

3. List dynamic library dependency of my own C++ program compiled from hello.cpp. I see 3 more libraries.

herong$ g++ -o hello hello.cpp

herong$ ./hello
Hello world! From C++.

herong$ ldd hello
  linux-vdso.so.1 (0x00007ffec33c2000)
  libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007fc85b58c000)
  libm.so.6 => /lib64/libm.so.6 (0x00007fc85b20a000)
  libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007fc85aff2000)
  libc.so.6 => /lib64/libc.so.6 (0x00007fc85ac2f000)
  /lib64/ld-linux-x86-64.so.2 (0x00007fc85b921000)

4. List dynamic library dependency of my own program that uses my own dynamic library. I see my dynamic library libMyDynamic.so in the list.

herong$ g++ -L . -lMyDynamic -o MyDynamicTest MyDynamicTest.cpp

herong$ export LD_LIBRARY_PATH=.

herong$ ./MyDynamicTest
Calling myDynamicFunc()...
Running myDynamicFunc()...

herong$ ldd MyDynamicTest
  linux-vdso.so.1 (0x00007ffe01126000)
  libMyDynamic.so => ./libMyDynamic.so (0x00007ff04c617000)
  libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007ff04c282000)
  libm.so.6 => /lib64/libm.so.6 (0x00007ff04bf00000)
  libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007ff04bce8000)
  libc.so.6 => /lib64/libc.so.6 (0x00007ff04b925000)
  /lib64/ld-linux-x86-64.so.2 (0x00007ff04c819000)

5. If LD_LIBRARY_PATH environment variable is not set properly, some dynamic libraries will be listed as "not found".

herong$ export LD_LIBRARY_PATH=/tmp

herong$ ldd MyDynamicTest
  linux-vdso.so.1 (0x00007ffe9e734000)
  libMyDynamic.so => not found
  libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007efc7393e000)
  libm.so.6 => /lib64/libm.so.6 (0x00007efc735bc000)
  libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007efc733a4000)
  libc.so.6 => /lib64/libc.so.6 (0x00007efc72fe1000)
  /lib64/ld-linux-x86-64.so.2 (0x00007efc73cd3000)

6. "ldd" works on dynamic library files (*.so files) too, because they are executable files.

herong$ g++ -shared -fPIC -o libMyDynamic.so MyDynamic.cpp

herong$ ldd libMyDynamic.so
  linux-vdso.so.1 (0x00007ffd7f35d000)
  libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007f9e27b19000)
  libm.so.6 => /lib64/libm.so.6 (0x00007f9e27797000)
  libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f9e2757f000)
  libc.so.6 => /lib64/libc.so.6 (0x00007f9e271bc000)
  /lib64/ld-linux-x86-64.so.2 (0x00007f9e280b0000)

7. Sorry, "ldd" does not work on object files (.o files), because they are not executable files.

herong$ g++ -c -o hello.o hello.cpp

herong$ ldd hello.o
ldd: warning: you do not have execution permission for './hello.o'
  not a dynamic executable

Table of Contents

 About This Book

 Introduction to Linux Systems

 Process Management

 Files and Directories

 Running Apache HTTP Server (httpd) on Linux Systems

 Running Apache Tomcat on Linux Systems

 Running PHP Scripts on Linux Systems

 Running MySQL Database Server on Linux Systems

 Running Python Scripts on Linux Systems

 Conda - Environment and Package Manager

GCC - C/C++ Compiler

 Install GCC C/C++ Compilers

 "g++ --verbose" - GCC Compiler Steps and Settings

 "g++ -I..." and CPATH Environment Variable

 "g++ -l..." to Link with Library Files

 "g++ -c" and "ar src" to Build Static Library

 "g++ -shared" to Build Dynamic Library

"ldd" - Dynamic Library Dependency Checker

 "make" - Manage Program Build Process

 OpenJDK - Open-Source JDK

 Graphics Environments on Linux

 SquirrelMail - Webmail in PHP

 Tools and Utilities

 References

 Full Version in PDF/EPUB