Are you adding or removing a new library to your program? There are situations where dynamic linker may not be aware of it.
In this tutorial, we learn about ldconfig command to manage dynamic linker run-time bindings in Linux.
ldconfig is a Linux command that is used to configure dynamic linker run-time bindings. The dynamic linker is in charge of loading and linking libraries to applications at run time.
ldconfig command is used to manage the list of libraries that the dynamic linker searches when loading libraries for dynamically linked programs. It checks for the shared library directories specified in the following places.
- /etc/ld.so.conf file
- directories specified in the command line (command-line mode with -n)
- trusted directories
- /lib,/usr/lib (32-bit systems)
- /lib64,/usr/lib64 (64-bit systems)
How ldconfig works
The ldconfig tool uses the ld.so.conf file to decide which directories to look for libraries when it is executed. It searches for libraries that have the .so suffix (signifying that they are shared libraries) and generates symbolic links for them in the /etc/ld.so.cache directory for each directory given in the ld.so.conf file.
The format of the symbolic links produced by ldconfig is yourlibname.so.x, where yourlibname denotes the name of the library and x denotes the version. The dynamic linker looks up the locations of libraries at runtime using the ld.so.cache file.
The dynamic linker looks through the ld.so.cache file for the necessary libraries before loading them into memory when a dynamically linked program is run.
ldconfig is available by default in most Linux Distributions. In case you get the "ldconfig command not found", install the respective Glibc package.
Ubuntu / Debian
apt install glibc-source #Debian
apt install eglibc-source #Ubuntu
Redhat / RHEL
yum install glibc
dnf install glibc
pacman -S glibc
How to use ldconfig
Let's look at how to use ldconfig command with some useful examples.
Print libraries stored in the current cache
When you add a new shared library to the system, the ldconfig command creates the symbolic links and updates the cache as well. The -p option will print all the shared objects stored in the current cache as follows.
Related Read: 8 LDD Command Examples in Linux
Manually link libraries
Use the -n option to specify that the specified libraries should be added to the list of libraries that are searched by the dynamic linker, without actually creating any symbolic links.
For example, to add the /usr/local/lib/mynewlib.so library to the list of libraries searched by the dynamic linker, you could use the following command:
ldconfig -n /usr/local/lib/mynewlib.so
Update the Cache
When you installed a new library, the ldconfig command can be used with the -n option to directly update the links for the shared library. But this doesn't update the cache. It is mandatory to run the following command to rebuild and update the cache.
If the command is executed successfully, no output will print to the console.
All the available shared libraries in the system can be examined by executing the ldconfig command along with the -v option.
ldconfig command options
-p - Prints all the directories and shared libraries stored in the current cache file /etc/ld.so.cache. Additionally, prints the number of libraries found in the cache as well.
-v - The verbose mode prints the version numbers of each library, directory names, and links associated with those. It overrides the normal quiet mode.
-n - Enables the command line mode and the command will only process directories specified in the command line. The trusted directories or the ones specified in the /etc/ld.so.conf won't process.
-V - Prints the ldconfig program version.
-N - Halts the cache file (ld.so.cache) update.
-X - Stops updating the links.
-f - Use the configuration file specified with this option instead of the default one (/etc/ld.so.conf).
-l - Enables linking the individual libraries manually. This is called library mode.
-C Use the cache file specified with this option instead of the default one (/etc/ld.so.cache)
-i - Auxiliary cache file will ignore.
Typically during the makefiles process, the shared library cache is updated. ldconfig usually comes to help when the library got changed or newer versions are installed.
Thanks for reading, please leave your feedback and suggestions in the below comment section.
Navigate all-in-one place of Linux Commands for more learning.