The commands ldd and ldconfig are the Linux tools for managing shared libraries. You might have noticed many files starting with lib* in /lib and /usr/lib directories of your Linux machine. These files are called libraries. A library is a collection of objects, functions etc. A library makes it possible for a program to use common routines without the administrative overhead of maintaining their source code, or the processing overhead of compiling them each time the program is compiled.
There are mainly two types of libraries in Linux. They are:
1. Static library.
These are the libraries with “.a” extension. These libraries are included (a separate copy) into the programs that require its functions.
2. Dynamic library.
These are the libraries with “.so” extension. These libraries will be linked to the program while executing the program that requires it. That is, there will not be separate copies of libraries for the programs using it, rather than it dynamically links to it. In this article, we will go through the commands “ldd” and “ldconfig” which are used to manage the shared libraries.
Please note down the following points before going through the command:
- The file, ld-linux.so is the dynamic linker or loader which checks for the desired link or library cache for the requested program and loads it.
- The cache file, /etc/ld.so.cache contains a list of libraries found in the directories specified in /etc/ld.so.conf. This helps to provide faster dynamic linking.
- The file /etc/ld.so.conf specifies the directories where to search for libraries
ldconfig creates the necessary links and cache (for use by the run-time linker, ld.so) to the most recent shared libraries found in the directories specified on the command line, in the file/etc/ld.so.conf, and in the trusted directories (/usr/lib and /lib).
Execute the following command to set up the correct links for the shared binaries and rebuild the cache.
# ldconfig –v
Execute the following command after the installation of a new shared library will properly update the shared library symbolic links in /lib.
# ldconfig -n /lib
Following command will print the current cache.
Common shared library related errors.
1. Missing library error.
You may encounter missing library error even though the mentioned libraries are available in the new installation path “/opt/newinstall/lib”. This is because the system is not aware of this directory to check for libraries. This can be fixed in any of the two ways.
a. Execute the following command,
# ldconfig -n /opt/newinstall/lib
b. You can see the following include line in /etc/ld.so.conf file:
So, create a file in /etc/ld.so.sonf.d folder, say newinstall.conf with the following content.
2. Dynamic linker error, can’t map cache files.
This may be due to corrupted cache file. This can be resolved by rebuilding the cache file using ldconfig.
ldd prints the shared libraries required by each program or shared library specified on the command line.
[root@localhost root]# ldd /bin/mv
libacl.so.1 => /lib/libacl.so.1 (0×40016000)
libc.so.6 => /lib/libc.so.6 (0x4001c000)
libattr.so.1 => /lib/libattr.so.1 (0×40141000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0×40000000)
# ldd -d execute_file
Perform relocations and report any missing objects (ELF only).
# ldd -r execute file
Perform relocations for both data objects and functions, and report any missing objects or functions (ELF only).