Department of Engineering

IT Services

linux shared libraries

Note: Nearly all of this is from http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html

Shared libraries are collections of code that can be shared by programs. Unlike static (aka archive) libraries, they're not part of the compiled program - they're loaded in at the start of run-time if they're not already being used. You don't need to worry about them unless you're a system manager or applications writer.

Names

Every shared library has

  • a "soname" - libname.so.version_number - the version number is incremented whenever the interface changes
  • a "real name" - the filename containing the actual library code. The "real name" adds to the "soname" a period, a minor number, another period, and the release number. The last period and release number are optional.
  • a "linker name" - the name that the compiler uses (the soname without any version number).

Creating shared libraries

  • To generate object files, use -fPIC or -fpic
  • To create a library, use cc -shared -Wl,-soname,your_soname -o library_name file_list library_list
  • To install, put the shared library in one of the special directories then run ldconfig. This creates the "soname" as a symbolic link to the "real name" and sets up the cache file /etc/ld.so.cache. You also need to create the "linker name" - a symbolic link to the soname

Shared libraries at run-time

On Linux, starting up an ELF binary executable automatically causes the program loader /lib/ld-linux.so.X to be loaded and run, which in turn, finds and loads all other shared libraries used by the program. The list of directories to be searched is stored in the file /etc/ld.so.conf (our file lists about 17 directories).

  • Use the LD_LIBRARY_PATH environmental variable to change run-time behaviour
  • Use "ldd program filename" to see the list of the sonames being depended on, along with the directory that those names resolve to.
  • Use export LD_DEBUG=files to print out information when you run subsequent commands

Note that libc.so.N is the C library, which is used by just about all programs.

libtool

Libtool is a utility that hides the complexity of using shared and static libraries. The *.la files in library directories are produced by it.