In Linux systems, we constantly work with executable files either in the terminal or graphical applications. Executables contain shared libraries, which are files that are shared and reused across programs. In windows, these are typically in the form of DDL files. In Linux, however, they are in the form of .o or .so files.

This guide will show you how to use the ldd command-line utility to show shared objects and the dependencies in an executable.

What is Ldd

Ldd is a powerful command-line tool that allows users to view an executable file’s shared object dependencies. A library refers to one or more pre-compiled resources such as functions, subroutines, classes, or values. Each of these resources is combined to create libraries.

In Linux, library files are commonly located in /lib or /usr/lib directories. Libraries help save time because programs can repurpose and use them as they see fit.

There are two types of libraries:

  • Static libraries
  • Dynamic or Shared Libraries

How to Install The Ldd command

Typically, you will find the ldd command available in almost any Linux distribution. However, in case it is unavailable, you can install it by using the command:

sudo apt-get install libc-bin

With the command installed, we can start using it.

Basic Ldd Command Usage

The general syntax for the ldd command is pretty straightforward. By default, executing the ldd command shows the shared object dependencies.

The syntax is as:

For example, to show the shared library dependencies of the bash binary, we use the command:

The above command shows the dependencies as:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image1-56.png" data-lazy- height="106" src="data:image/svg xml,” width=”817″>

The first section shows a virtual dynamic shared object (VDSO). The second line shows the path of the ELF interpreter that is hardcoded into the executable, and the final part shows the memory at which the library is loaded.

Ldd Options

We use ldd options to modify the behavior of the command. Supported options include:

  • -v – Verbose mode, which prints all information.
  • -u – Shows unused direct dependencies
  • -d – Executes data relocation and shows missing ELF objects.
  • -r – executes data and function relocation and shows missing ELF objects and functions.

Show detailed output

To show the dependencies of a binary with detailed information, we can pass the -v flag to the ldd command as:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image3-56.png" data-lazy- height="686" src="data:image/svg xml,” width=”1000″>

Ldd Show Unused Direct Dependencies

Show unused direct dependencies with the -u flag as:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image2-55.png" data-lazy- height="61" src="data:image/svg xml,” width=”489″>

NOTE: Ldd only works with dynamic executables. If used on a non-dynamic executable, it will display an error as:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image4-54.png" data-lazy- height="37" src="data:image/svg xml,” width=”376″>

In Conclusion

This tutorial covered how to use the ldd command in Linux and get shared library dependency information for dynamic executables. If you learned something from this tutorial, share it with others.

If you are looking for detailed information, consider the following man pages:

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/john-150×150.png60d31be076918.jpg" height="112" src="data:image/svg xml,” width=”112″>

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list