Shared Libraries

What are shared Libraries?

Shared Libraries are an area where object code has been compiled and grouped generally into a single file (library). In many cases, code that can be shared by more than one application can be removed and compiled into a shared area. Generally there are two types of shared libraries:

Static Libraries

Statically linked programs tend to get large because they include executable files for all of the library functions linked into them. Statically Linked programs often use more memory when other programs that are currently running contain the same library functions. Statically Linked Libraries are identified by an extension of ".a". To avoid some of the issues encountered by using static libraries, many programs will use what is known as "Dynamically Linked" libraries.

Dynamic Libraries

Dynamically Linked libraries allow multiple programs to share the same library code in memory which results in a smaller memory footprint. Dynamically linked libraries are shared amongst multiple programs. The term "Shared Libraries" often relates to dynamically linked libraries. Dynamic Libraries have the extension of ".so".

Displaying Shared Library Dependencies

The "ldd" command can be used to print the shared libraries required by each program or shared library specified on the command.

ldd command example

In the example below we will use the "ldd" command to display the shared libraries associated with the "/bin/bash program.


# ldd /bin/bash
	linux-vdso.so.1 (0x00007ffed0576000)
	libtinfo.so.6 => /lib/x86_64-linux-gnu/libtinfo.so.6 (0x00007ff2b4f4f000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007ff2b4f49000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff2b4d57000)
	/lib64/ld-linux-x86-64.so.2 (0x00007ff2b50c9000)

Syntax: Syntax: ldd [OPTION]... FILE...


Usage: ldd [OPTION]... FILE...
      --help              print this help and exit
      --version           print version information and exit
  -d, --data-relocs       process data relocations
  -r, --function-relocs   process data and function relocations
  -u, --unused            print unused direct dependencies
  -v, --verbose           print all information

Linking Shared Libraries

Dynamically linked executables are examined at their runtime by "ld.so" the Shared Object Dynamic Linker. "ld.so" checks for any dependencies in the executable and then attempts to satisfy these. If ld.so is unable to satisfy these dependencies, then the executable will fail.

Library Path

For an executable to find the necessary libraries to link with at its run time, the system has to be configured so that these libraries can be found. There are several ways this can be achieved.

/etc/ld.so.conf

Add your library directory into the location "/etc/ld.so.conf". Once your entry has been added, you must issue the "ldconfig" command as the root user to complete the process.

LD_LIBRARY_PATH

Use the environment variable "LD_LIBRARY_PATH" to point to the directory paths containing the shared object library. Multiple directories can be added separated by a colon ":".

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/lib; export LD_LIBRARY_PATH

This entry can be added into your .bashrc file located in your home directory. (Amend accordingly)

Below is an example of an entry containing multiple entries:

LD_LIBRARY_PATH=LD_LIBRARY_PATH:/u00/app/oracle/product/12.1.0/client_1/lib:/schedule/ctmauser/ctm/exe

ldconfig command

ldconfig is used to configure dynamic linker run time bindings. ldconfig creates the necessary links and cache to the most recent shared libraries found in the directories specified from the command line, in the file "/etc/ld.so.conf", and in the trusted directories (/lib and /usr/lib). This cache is used by the run time linker "ld.so". ldconfig must be run when any changes are made to the file "/etc/ld.so.conf". This will refresh the cache of directories.


Usage: ldconfig.real [OPTION...]
Configure Dynamic Linker Run Time Bindings.

  -c, --format=FORMAT        Format to use: new (default), old, or compat
  -C CACHE                   Use CACHE as cache file
  -f CONF                    Use CONF as configuration file
  -i, --ignore-aux-cache     Ignore auxiliary cache file
  -l                         Manually link individual libraries.
  -n                         Only process directories specified on the command
                             line.  Don't build cache.
  -N                         Don't build cache
  -p, --print-cache          Print cache
  -r ROOT                    Change to and use ROOT as root directory
  -v, --verbose              Generate verbose messages
  -X                         Don't update symbolic links
  -?, --help                 Give this help list
      --usage                Give a short usage message
  -V, --version              Print program version


Files associated with the ldconfig command

/lib/ld.so - Run Time linker/loader

/etc/ld.so.conf - File containing a list of colon, space, tab, newline, or comma-separated directories in which to search for libraries.

/etc/ld.so.cache - File containing an ordered list of libraries found in the directories specified in /etc/ld.so.conf.