Linux supports two classes of libraries, namely:

  • Static libraries – are bound to a program statically at compile time.
  • Dynamic or shared libraries – are loaded when a program is launched and loaded into memory and binding occurs at run time.


Dynamic or shared libraries can further be categorized into:

  • Dynamically linked libraries – here a program is linked with the shared library and the kernel loads the library (in case it’s not in memory) upon execution.
  • Dynamically loaded libraries – the program takes full control by calling functions with the library.


Shared Library Naming Conventions


Shared libraries are named in two ways: the library name (a.k.a soname) and a “filename” (absolute path to file which stores library code).


For example, the soname for libc is where lib is the prefix, c is a descriptive name, so means shared object, and 6 is the version. And its filename is: /lib64/ Note that the soname is actually a symbolic link to the filename.


Locating Shared Libraries in Linux


Shared libraries are loaded by (or and (or programs, where x is the version. In Linux, /lib/ searches and loads all shared libraries used by a program.


A program can call a library using its library name or filename, and a library path stores directories where libraries can be found in the filesystem. By default, libraries are located in /usr/local/lib, /usr/local/lib64, /usr/lib and /usr/lib64; system startup libraries are in /lib and /lib64. Programmers can, however, install libraries in custom locations.


The library path can be defined in /etc/ file which you can edit with a command-line editor.

# vi /etc/ 


The line(s) in this file instruct the kernel to load file in /etc/ This way, package maintainers or programmers can add their custom library directories to the search list.


If you look into the /etc/ directory, you’ll see .conf files for some common packages (kernel, mysql and postgresql in this case):

# ls /etc/

kernel-2.6.32-358.18.1.el6.x86_64.conf  kernel-2.6.32-696.1.1.el6.x86_64.conf  mariadb-x86_64.conf
kernel-2.6.32-642.6.2.el6.x86_64.conf   kernel-2.6.32-696.6.3.el6.x86_64.conf  postgresql-pgdg-libs.conf


If you take a look at the mariadb-x86_64.conf, you will see an absolute path to package’s libraries.

# cat mariadb-x86_64.conf



The method above sets the library path permanently. To set it temporarily, use the LD_LIBRARY_PATH environment variable on the command line. If you want to keep the changes permanent, then add this line in the shell initialization file /etc/profile (global) or ~/.profile (user specific).

# export LD_LIBRARY_PATH=/path/to/library/file


Managing Shared Libraries in Linux


Let us now look at how to deal with shared libraries. To get a list of all shared library dependencies for a binary file, you can use the ldd utility. The output of ldd is in the form:

library name =>  filename (some hexadecimal value)
filename (some hexadecimal value)  #this is shown when library name can’t be read


This command shows all shared library dependencies for the ls command.

# ldd /usr/bin/ls
# ldd /bin/ls


Sample Output:
-------------------------------------------------------------------------------------------------------- => (0x00007ffebf9c2000) => /lib64/ (0x0000003b71e00000) => /lib64/ (0x0000003b71600000) => /lib64/ (0x0000003b76a00000) => /lib64/ (0x0000003b75e00000) => /lib64/ (0x0000003b70600000) => /lib64/ (0x0000003b70a00000) /lib64/ (0x0000561abfc09000) => /lib64/ (0x0000003b70e00000) => /lib64/ (0x0000003b75600000)


Because shared libraries can exist in many different directories, searching through all of these directories when a program is launched would be greatly inefficient: which is one of the likely disadvantages of dynamic libraries. Therefore a mechanism of caching employed, performed by a the program ldconfig.


By default, ldconfig reads the content of /etc/, creates the appropriate symbolic links in the dynamic link directories, and then writes a cache to /etc/ which is then easily used by other programs.


This is very important especially when you have just installed new shared libraries or created your own, or created new library directories. You need to run ldconfig command to effect the changes.

# ldconfig
# ldconfig -v 	#shows files and directories it works with


After creating your shared library, you need to install it. You can either move it into any of the standard directories mentioned above, and run the ldconfig command.


Alternatively, run the following command to create symbolic links from the soname to the filename:

# ldconfig -n /path/to/your/shared/libraries


Was this answer helpful? 0 Users Found This Useful (0 Votes)