Linux ldd Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux ldd command, which is a useful tool for identifying the dynamic library dependencies of a binary file. We will start by understanding the purpose of the ldd command, and then learn how to use it to troubleshoot missing dependencies. This knowledge is essential for effective software management and troubleshooting on Linux systems.

The lab will cover the following steps:

  1. Understand the Purpose of the ldd Command
  2. Identify Dynamic Dependencies of a Binary
  3. Troubleshoot Missing Dependencies

The ldd command is a valuable tool, but it's important to note that it may require additional packages to be installed on some Linux distributions, and it has been deprecated in favor of more modern tools on some newer Linux versions. However, it remains a widely used and essential command for many Linux users and administrators.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/FileandDirectoryManagementGroup -.-> linux/find("`File Searching`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") subgraph Lab Skills linux/find -.-> lab-422757{{"`Linux ldd Command with Practical Examples`"}} linux/ls -.-> lab-422757{{"`Linux ldd Command with Practical Examples`"}} end

Understand the Purpose of the ldd Command

In this step, we will explore the purpose of the ldd command in Linux. The ldd command is a tool that allows you to identify the dynamic library dependencies of a binary file.

First, let's create a simple C program that uses a dynamic library:

// hello.c
#include <stdio.h>
#include <mylib.h>

int main() {
    print_message();
    return 0;
}

Now, let's compile the program and use ldd to check its dependencies:

$ gcc -o hello hello.c -L. -lmylib
$ ldd hello
        linux-vdso.so.1 (0x00007ffff7ffd000)
        libmylib.so => not found
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7f7a5f0000)
        /lib64/ld-linux-x86-64.so.2 => /lib64/ld-linux-x86-64.so.2 (0x00007f7f7a7c0000)

The output shows that the hello binary depends on the libmylib.so dynamic library, which is not found. This is a common issue when working with dynamic libraries, and the ldd command can help you identify and troubleshoot such problems.

Example output:

        linux-vdso.so.1 (0x00007ffff7ffd000)
        libmylib.so => not found
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7f7a5f0000)
        /lib64/ld-linux-x86-64.so.2 => /lib64/ld-linux-x86-64.so.2 (0x00007f7f7a7c0000)

The ldd command is a valuable tool for understanding the dynamic dependencies of a binary, which is essential for effective software management and troubleshooting.

Identify Dynamic Dependencies of a Binary

In this step, we will learn how to use the ldd command to identify the dynamic dependencies of a binary file.

First, let's create a simple C program that uses a dynamic library:

// hello.c
#include <stdio.h>
#include <mylib.h>

int main() {
    print_message();
    return 0;
}

Now, let's compile the program and use ldd to check its dependencies:

$ gcc -o hello hello.c -L. -lmylib
$ ldd hello
        linux-vdso.so.1 (0x00007ffff7ffd000)
        libmylib.so => not found
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7f7a5f0000)
        /lib64/ld-linux-x86-64.so.2 => /lib64/ld-linux-x86-64.so.2 (0x00007f7f7a7c0000)

The output shows that the hello binary depends on the libmylib.so dynamic library, which is not found. This means that the hello binary will not be able to run correctly until the missing dependency is resolved.

Example output:

        linux-vdso.so.1 (0x00007ffff7ffd000)
        libmylib.so => not found
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7f7a5f0000)
        /lib64/ld-linux-x86-64.so.2 => /lib64/ld-linux-x86-64.so.2 (0x00007f7f7a7c0000)

The ldd command is a powerful tool for identifying the dynamic dependencies of a binary file, which is essential for troubleshooting and managing software dependencies.

Troubleshoot Missing Dependencies

In this step, we will learn how to troubleshoot missing dependencies using the ldd command.

Let's continue with the example from the previous step. We have a hello binary that depends on the libmylib.so dynamic library, but the library is not found.

First, let's try to locate the missing library:

$ sudo find / -name libmylib.so
/usr/local/lib/libmylib.so

The library is located in the /usr/local/lib directory. Now, let's try running the hello binary again:

$ ldd hello
        linux-vdso.so.1 (0x00007ffff7ffd000)
        libmylib.so => /usr/local/lib/libmylib.so (0x00007f7f7a5f0000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7f7a7c0000)
        /lib64/ld-linux-x86-64.so.2 => /lib64/ld-linux-x86-64.so.2 (0x00007f7f7a8e0000)
$ ./hello
Hello, World!

The ldd command now shows that the libmylib.so library is found, and the hello binary runs successfully.

Example output:

        linux-vdso.so.1 (0x00007ffff7ffd000)
        libmylib.so => /usr/local/lib/libmylib.so (0x00007f7f7a5f0000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7f7a7c0000)
        /lib64/ld-linux-x86-64.so.2 => /lib64/ld-linux-x86-64.so.2 (0x00007f7f7a8e0000)

In this example, we were able to troubleshoot the missing dependency by locating the libmylib.so library and ensuring that it was available in the system's library search path.

The ldd command is a valuable tool for identifying and troubleshooting missing dependencies, which is essential for effective software management and deployment.

Summary

In this lab, we explored the purpose and usage of the ldd command in Linux. The ldd command is a valuable tool for identifying the dynamic library dependencies of a binary file, which is essential for effective software management and troubleshooting. We created a simple C program that uses a dynamic library, and then used ldd to check its dependencies. The output showed that the program depends on the libmylib.so dynamic library, which was not found, highlighting a common issue when working with dynamic libraries. Understanding the dynamic dependencies of a binary is crucial for resolving such problems and ensuring the proper functioning of software applications.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like