Linux ldd Command

Linux 命令大全Linux Command Encyclopedia


ldd (List Dynamic Dependencies) is a very useful command-line tool in Linux systems, used to display the dynamic link libraries that a program or shared library depends on. When you run a program on Linux, the system needs to load various shared libraries (.so files) that the program depends on; ldd is the tool used to view these dependency relationships.


Basic syntax of the ldd command

ldd [选项] 可执行文件或共享库

Common options and parameter descriptions

Option Description
-v Display detailed version information
-u Display unused direct dependencies
-d Perform relocation and report missing functions
-r Perform relocation and report missing functions and data
--help Display help information

How the ldd command works

ldd is actually a script that runs the target program by setting special environment variables, so that the program displays its dependent library information when loading instead of executing normally. Specifically:

  1. For ELF-format executables, ldd parses their dynamic segment.
  2. It checks the DT_NEEDED entries to determine the required shared libraries.
  3. It searches for these library files in the system library paths.
  4. It displays the full path and memory address mapping of each library.

Usage examples

Example 1: Viewing the dependencies of a simple program

ldd /bin/ls

Typical output:

linux-vdso.so.1 (0x00007ffd5a3f0000)
libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f8e4a3b0000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f8e4a1c0000)
libpcre2-8.so.0 => /usr/lib/x86_64-linux-gnu/libpcre2-8.so.0 (0x00007f8e4a130000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f8e4a120000)
/lib64/ld-linux-x86-64.so.2 (0x00007f8e4a400000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f8e4a100000)

Example 2: Checking unused direct dependencies

ldd -u /usr/bin/python3

Example 3: Viewing dependencies in verbose mode

ldd -v /usr/bin/gcc

Practical application scenarios

Scenario 1: Resolving "library not found" errors

When a program cannot run because a library is missing, ldd can help quickly locate the problem:

ldd ./my_program | grep "not found"

Scenario 2: Checking library version conflicts

ldd -r ./my_program

Scenario 3: Verifying whether the library path is correct

ldd /path/to/your/executable | awk '{print $3}' | xargs ls -la

Notes and common issues

  1. Security warning: Do not use ldd on untrusted executables, because it actually attempts to execute parts of the program's code.

    • Alternatives:objdump -p /path/to/program | grep NEEDED
  2. Statically linked programs: For fully statically linked programs, ldd will display "not a dynamic executable".

  3. Cross-compilation environments: In cross-compilation environments, you need to use the ldd tool corresponding to the target platform.

  4. Common errors:

    • ldd: ./program: No such file or directory→ May be due to a missing interpreter or the program itself does not exist
    • ldd: exited with unknown exit code→ The program may crash during runtime

Advanced tips

1. Use readelf to view more detailed dependency information

readelf -d /path/to/program | grep NEEDED

2. Use the LD_TRACE_LOADED_OBJECTS environment variable

LD_TRACE_LOADED_OBJECTS=1 /path/to/program

3. Check the library dependency tree

ldd /path/to/library.so | awk '{print $3}' | xargs ldd

Summary

ldd is an indispensable tool in Linux system administration and program debugging. It can:

  • Quickly display the dynamic library dependency relationships of a program
  • Help resolve missing library or version conflict issues
  • Verify whether the program's runtime environment is complete
  • Assist in software packaging and deployment

Mastering the use of the ldd command can greatly improve your efficiency in solving dependency problems in the Linux environment.


Linux 命令大全Linux Command Encyclopedia

Other extensions