Online Book Reader

Home Category

Running Linux, 5th Edition - Matthias Kalle Dalheimer [231]

By Root 1138 0
. These libraries contain useful functions common to many programs. Instead of storing a copy of these routines in each program that calls them, the libraries are contained in files on the system that are read by all programs at runtime. That is, when a program is executed, the code from the program file itself is read, followed by any routines from the shared library files. This saves a great deal of disk space—only one copy of the library routines is stored on disk.

If you're lucky, using the package system means that the right versions of the libraries each program needs are installed along with the programs. The package system is supposed to be aware of dependencies on shared libraries. But because different programs may depend on different versions of libraries, or because you might install a program without using the package system, you occasionally have to understand the conventions for libraries explained in this section.

In some instances, it's necessary to compile a program to have its own copy of the library routines (usually for debugging) instead of using the routines from the shared libraries. We say that programs built in this way are statically linked, whereas programs built to use shared libraries are dynamically linked.

Therefore, dynamically linked executables depend upon the presence of the shared libraries on disk. Shared libraries are implemented in such a way that the programs compiled to use them generally don't depend on the version of the available libraries. This means that you can upgrade your shared libraries, and all programs that are built to use those libraries will automatically use the new routines. (There is an exception: if major changes are made to a library, the old programs won't work with the new library. You'll know this is the case because the major version number is different; we explain more later. In this case, you keep both the old and new libraries around. All your old executables will continue to use the old libraries, and any new programs that are compiled will use the new libraries.)

When you build a program to use shared libraries, a piece of code is added to the program that causes it to execute ld.so, the dynamic linker, when the program is started. ld.so is responsible for finding the shared libraries the program needs and loading the routines into memory. Dynamically linked programs are also linked against "stub" routines, which simply take the place of the actual shared library routines in the executable. ld.so replaces the stub routine with the code from the libraries when the program is executed.

The ldd command can be used to list the shared libraries on which a given executable depends. For example:

rutabaga$ ldd /usr/bin/X11/xterm

linux-gate.so.1 => (0xffffe000)

libXft.so.2 => /usr/X11R6/lib/libXft.so.2 (0x40037000)

libfontconfig.so.1 => /usr/lib/libfontconfig.so.1 (0x4004b000)

libfreetype.so.6 => /usr/lib/libfreetype.so.6 (0x40079000)

libexpat.so.0 => /usr/lib/libexpat.so.0 (0x400e8000)

libXrender.so.1 => /usr/X11R6/lib/libXrender.so.1 (0x40107000)

libXaw.so.8 => /usr/X11R6/lib/libXaw.so.8 (0x4010f000)

libXmu.so.6 => /usr/X11R6/lib/libXmu.so.6 (0x4016b000)

libXt.so.6 => /usr/X11R6/lib/libXt.so.6 (0x40182000)

libSM.so.6 => /usr/X11R6/lib/libSM.so.6 (0x401d5000)

libICE.so.6 => /usr/X11R6/lib/libICE.so.6 (0x401dd000)

libXpm.so.4 => /usr/X11R6/lib/libXpm.so.4 (0x401f5000)

libXp.so.6 => /usr/X11R6/lib/libXp.so.6 (0x40205000)

libXext.so.6 => /usr/X11R6/lib/libXext.so.6 (0x4020d000)

libX11.so.6 => /usr/X11R6/lib/libX11.so.6 (0x4021c000)

libncurses.so.5 => /lib/libncurses.so.5 (0x40318000)

libutempter.so.0 => /usr/lib/libutempter.so.0 (0x4035d000)

libc.so.6 => /lib/tls/libc.so.6 (0x4035f000)

libdl.so.2 => /lib/libdl.so.2 (0x40478000)

/lib/ld-linux.so.2 (0x40000000)

Here, we see that the xterm program depends on a number of shared libraries, including libXaw, libXt, libX11, and libc. (The libraries starting with libX as well as libSM and libICE are all related to the X Window System; libc is the standard C library.) We also see the version

Return Main Page Previous Page Next Page

®Online Book Reader