The Linux Programming Interface ยท TLPI
Chapter 41: Fundamentals of Shared Libraries
Deep dive into shared library mechanics on Linux โ linking, loading, sonames & versioning
๐
2 Tutorial Pages
๐ป
8 Code Examples
๐ฏ
16 Interview Q&As
๐ง
Sections 41.4.3โ41.4.4
About This Chapter
This chapter series covers the fundamental mechanics of shared libraries (also called dynamic libraries or shared objects) on Linux. You will learn:
- How a shared library name gets embedded inside an ELF executable at link time
- How the dynamic linker (ld-linux.so.2) loads shared libraries at runtime
- How LD_LIBRARY_PATH works and when NOT to use it
- What a soname is and how it enables library versioning
- How to create a shared library with a soname and manage the symbolic link chain
- How to upgrade a library without recompiling any programs
Prerequisites: Basic C programming, familiarity with gcc, and understanding of what a compiled object file is.
Chapter Keywords
Shared Library (.so) Static Library (.a) Dynamic Linker ld-linux.so.2 ELF DT_NEEDED DT_SONAME LD_LIBRARY_PATH RPATH / $ORIGIN Soname Real Name Linker Name Symbolic Links ldconfig readelf objdump -fPIC -Wl,-soname ABI ldd
Tutorial Pages
Part 1 โ Using a Shared Library Section 41.4.3
Learn how shared libraries are embedded in ELF executables, how the dynamic linker works, what LD_LIBRARY_PATH does, RPATH and $ORIGIN, and static vs dynamic linking compared.
DT_NEEDED Tag Dynamic Linker LD_LIBRARY_PATH RPATH / $ORIGIN Static vs Dynamic Linking ldd / readelf / objdump -fPIC 8 Interview Questions
Start Reading โ
Part 2 โ The Shared Library Soname Section 41.4.4
Understand what a soname is, how to create a shared library with a soname, the three names of a shared library, symbolic link chains, and how to manage library versioning with major/minor version bumps.
Soname (DT_SONAME) Three Library Names -Wl,-soname Symbolic Link Chain Library Versioning ABI Compatibility ldconfig 8 Interview Questions
Start Reading โ
โก Quick Reference โ Most Important Commands
# Create shared library with soname
gcc -g -shared -fPIC -Wl,-soname,libfoo.so.1 -o libfoo.so.1.2.3 *.o
# Create symbolic link chain
ln -s libfoo.so.1.2.3 libfoo.so.1 # soname link
ln -s libfoo.so.1 libfoo.so # linker name link
# Link a program against a shared library
gcc -g -Wall -o prog prog.c -L. -lfoo
# Run with non-standard library directory
LD_LIBRARY_PATH=/path/to/libs ./prog
# Embed RPATH (production alternative)
gcc -g -Wall -o prog prog.c -L. -lfoo -Wl,-rpath,/path/to/libs
# Inspect embedded library names
readelf -d prog | grep NEEDED
ldd prog
# Check library soname
readelf -d libfoo.so.1.2.3 | grep SONAME
# Update ldconfig cache (after installing to /usr/lib etc.)
sudo ldconfig
Start Learning
Begin with Part 1 to understand the basics of using shared libraries
