Getting ready

By convention, library files start with the lib prefix. All libraries need to be explicitly passed to the linker so that they are used, except libc which will be automatically linked. There are basically two library types:

  • Static libraries (.a): When the object code is linked and becomes part of the application.
  • Dynamic libraries (.so): These are linked at compile time but not included in the application, so they need to be available at runtime. Multiple applications can share a dynamic library so they need less disk space.

Libraries are typically placed in the following standard root filesystem locations:

  • /lib: Libraries required for startup
  • /usr/lib: Most system libraries
  • /usr/local/lib: Non-system libraries

Dynamic libraries follow certain naming conventions on running systems so that multiple versions can co-exist; thus a library can be referenced by different names. Some of them are explained next:

  • The linker name with the .so suffix; for example, libexample.so.
  • The fully qualified name or soname, a symbolic link to the library name. For example, libexample.so.x, where x is the version number. Increasing the version number means the library is not compatible with previous versions.
  • The real name. For example, libexample.so.x.y[.z], where x is the major version number, y is the minor version number, and the optional z is a release number. Increasing minor or release numbers retains compatibility.

So, for a given libexample library, we find the following:

  • libexample.so.x.y[.z], the actual shared library that needs to be present in both host and target
  • libexample.so.x, a symbolic link to the preceding file used in the target when loading the library at runtime
  • libexample.so, also a symbolic link as before used in the host when linking

In GNU glibc, starting an ELF binary calls a program loader, /lib/ld-linux-X. Here, X contains both an architecture string and the version number. The program loader finds all the needed shared libraries. This process uses a couple of interesting files:

  • /etc/ld.so.conf: This stores the directories searched by the loader
  • /etc/ld.so.preload: This is used to override libraries

The ldconfig tool reads the ld.so.conf file and creates a cache file (/etc/ld.so.cache) to increase access speed. The following environment variables can also be helpful:

  • LD_LIBRARY_PATH: This is a colon-separated directory list to search libraries in. It is used when debugging or using non-standard library locations.
  • LD_PRELOAD: This is used to override shared libraries.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset