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.


Syntax

ldd programs


Description

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.


Example

The bash shell requires three shared libraries:

# ldd /bin/bash
/bin/bash:
     libtermcap.so.2 => /lib/libtermcap.so.2 (0x40018000)
     libc.so.6 => /lib/libc.so.6 (0x4001c000)
     /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)

4.4.2. Linking Shared Libraries

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

To find a new library, ld.so 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 ld.so, files in the new directory must be included in an index of library names and locations. This index is /etc/ld.so.cache. It's a binary file, which means it can be read quickly by ld.so. To add the new library entry to the cache, first add its directory to the ld.so.conf file, which contains directories to be indexed by the ldconfig utility.


Syntax

ldconfig [options] lib_dirs


Description

Update the ld.so 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/ld.so.conf.


Frequently used options


-p

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


-v

Verbose mode. Display progress during execution.


Example 1

Examine the contents of the ld.so library cache:

# ldconfig -p
144 libs found in cache '/etc/ld.so.cache'
    libz.so.1 (libc6) => /usr/lib/libz.so.1
    libuuid.so.1 (libc6) => /lib/libuuid.so.1
    libutil.so.1 (libc6, OS ABI: Linux 2.2.5) => /lib/libutil.so.1
    libutil.so (libc6, OS ABI: Linux 2.2.5) => /usr/lib/libutil.so    libthread_db.so.1
(libc6, OS ABI: Linux 2.2.5) => /lib/libthread_db.so.1
    libthread_db.so (libc6, OS ABI: Linux 2.2.5) => /usr/lib/libthread_db.so
        libtermcap.so.2 (libc6) => /lib/libtermcap.so.2
(... listing continues ...)


Example 2

Look for a specific library entry in the cache:

# ldconfig -p | grep ncurses
        libncurses.so.5 (libc6) => /usr/lib/libncurses.so.5


Example 3

Rebuild the cache:

# ldconfig

After /usr/local/lib is added, ld.so.conf might look like this:

/usr/lib
/usr/i486-linux-libc5/lib
/usr/X11R6/lib
/usr/local/lib

Next, ldconfig is run to include libraries found in /usr/local/lib in /etc/ld.so.cache. 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