Previous Page
Next Page

4.4. Objective 4: Manage Shared Libraries

When a program is compiled under Linux, many of the functions required by the program are linked from system libraries that handle disks, memory, and other functions. For example, when printf( ) is used in a program, the programmer doesn't provide the printf( ) source code, but instead expects that the system already has a library containing such functions. When the compiler needs to link the code for printf( ), it can be found in a system library and copied into the executable. A program that contains executable code from these libraries is said to be statically linked because it stands alone, requiring no additional code at runtime.

Statically linked programs can have a few liabilities. First, they tend to get large because they include executables for all of the library functions linked into them. Also, memory is wasted when many different programs running concurrently contain the same library functions. To avoid these problems, many programs are dynamically linked. Such programs utilize the same routines but don't contain the library code. Instead, they are linked into the executable at runtime. This dynamic linking process allows multiple programs to use the same library code in memory and makes executable files smaller. Dynamically linked libraries are shared among many applications and are thus called shared libraries . A full discussion of libraries is beyond the scope of the LPIC Level 1 exams. However, a general understanding of some configuration techniques is required.

4.4.1. Shared Library Dependencies

Any program that is dynamically linked will require at least a few shared libraries. If the required libraries don't exist or can't be found, the program will fail to run. This could happen, for example, if you attempt to run an application written for the GNOME graphical environment but haven't installed the required GTK+ libraries. Simply installing the correct libraries should eliminate such problems. The ldd utility can be used to determine which libraries are necessary for a particular executable.


ldd programs


Display shared libraries required by each of the programs listed on the command line. The results indicate the name of the library and where the library is expected to be in the filesystem.


The bash shell requires three shared libraries:

# ldd /bin/bash
/bin/bash: => /lib/ (0x40018000) => /lib/ (0x4001c000)
     /lib/ => /lib/ (0x40000000)

4.4.2. Linking Shared Libraries

Dynamically linked executables are examined at runtime by the shared object dynamic linker, This program looks for dependencies in the executable being loaded and attempts to satisfy any unresolved links to system-shared libraries. If can't find a specified library, it fails, and the executable won't run.

To find a new library, must be instructed to look in /usr/local/lib. There are a few ways to do this. One simple way is to add a colon-separated list of directories to the shell environment variable LD_LIBRARY_PATH, which will prompt ld.soto look in any directories it finds there. However, this method may not be appropriate for system libraries, because users might not set their LD_LIBRARY_PATH correctly.

To make the search of /usr/local/lib part of the default behavior for, files in the new directory must be included in an index of library names and locations. This index is /etc/ It's a binary file, which means it can be read quickly by To add the new library entry to the cache, first add its directory to the file, which contains directories to be indexed by the ldconfig utility.


ldconfig [options] lib_dirs


Update the cache file with shared libraries specified on the command line in lib_dirs, in trusted directories /usr/lib and /lib, and in the directories found in /etc/

Frequently used options


Display the contents of the current cache instead of recreating it.


Verbose mode. Display progress during execution.

Example 1

Examine the contents of the library cache:

# ldconfig -p
144 libs found in cache '/etc/' (libc6) => /usr/lib/ (libc6) => /lib/ (libc6, OS ABI: Linux 2.2.5) => /lib/ (libc6, OS ABI: Linux 2.2.5) => /usr/lib/
(libc6, OS ABI: Linux 2.2.5) => /lib/ (libc6, OS ABI: Linux 2.2.5) => /usr/lib/ (libc6) => /lib/
(... listing continues ...)

Example 2

Look for a specific library entry in the cache:

# ldconfig -p | grep ncurses (libc6) => /usr/lib/

Example 3

Rebuild the cache:

# ldconfig

After /usr/local/lib is added, might look like this:


Next, ldconfig is run to include libraries found in /usr/local/lib in /etc/ It is important to run ldconfig after any changes in system libraries to be sure that the cache is up-to-date.

Previous Page
Next Page