Skip to main content
In this lesson, we explore how to manage shared libraries on Linux. You’ll learn about compilation vs. linking, static and dynamic libraries, naming conventions, library paths, dynamic linker configuration, and dependency inspection.

1. Compilation vs. Linking

Building an executable from source involves two key steps:
  1. Compilation
    The compiler (e.g., gcc) translates source files (.c, .cpp) into object files (.o).
  2. Linking
    The linker combines object files and connects them to libraries, producing the final executable.
Linking can be:
  • Static: Library code is embedded into the executable.
  • Dynamic: Executable references shared libraries at runtime.
The image is an educational slide explaining software libraries, compilers, linkers, and the concept of linking, including static and dynamic linking.

2. Static Libraries vs. Shared Libraries

Static Libraries (.a)

  • Merged into the executable at link time.
  • No runtime dependency on external files.
  • Larger binary size, but fully self-contained.
Static linking increases the size of the executable since all used library code is included.

Shared (Dynamic) Libraries (.so)

  • Executable holds references, not library code.
  • Resolved by the dynamic linker at runtime.
  • Multiple programs can share a single library instance in memory, saving disk and RAM.
The image explains the difference between static and shared libraries. Static libraries are merged with the program at link time with no runtime dependencies, while shared libraries are not merged and must be available at runtime.

3. Shared Library Naming Conventions

Shared libraries follow the SONAME pattern:
  • lib: prefix
  • .so: shared object
  • {major}: major version
Example:
The image shows a terminal interface with a section explaining naming conventions for shared libraries, including components like library name, shared object, and version number, with an example given as "libpthread.so.0".
On Debian 9.9, the GNU C library’s SONAME is libc.so.6, typically a symlink to the full version. Static archives end with .a, for example libpthread.a.

4. Common Library Locations

Shared libraries are usually installed in these directories:

5. Dynamic Linker Configuration

The dynamic linker (ld.so or ld-linux.so) reads /etc/ld.so.conf to locate libraries:
Each file in /etc/ld.so.conf.d/ lists absolute library directories, for example:
After editing, rebuild the cache and create necessary symlinks:
Sample output:
Query for a specific library:
Inspect the symlink:

6. LD_LIBRARY_PATH Environment Variable

LD_LIBRARY_PATH is a colon-separated list of directories the dynamic linker checks before standard paths.
To remove it:
Overusing LD_LIBRARY_PATH can cause version conflicts and security issues. Prefer updating /etc/ld.so.conf.d/ and running ldconfig for persistent changes.
For permanent updates, add the export line to ~/.bashrc or /etc/profile.

7. Inspecting Dependencies with ldd

Use ldd to list shared libraries an executable depends on:
Example output:
To list unused direct dependencies:
Example:
Unused entries often result from linker options during the build process.

References

Watch Video