Chapter 41: Fundamentals of Shared Libraries
Linux System Programming — TLPI Series | EmbeddedPathashala
3Sections
6HTML Files
12+Code Examples
20+Interview Q&A
What You Will Learn
When you write a C program that uses functions like printf(), malloc(), or sin(), those functions don’t magically appear in your binary. They live in libraries — collections of compiled code that your program can use. Linux supports two kinds of libraries: static libraries (code copied into your binary at link time) and shared libraries (code loaded into memory at run time and shared across multiple programs). This chapter teaches you everything about how these libraries work, how to build them, and why shared libraries are preferred in modern Linux development.
This series is based on The Linux Programming Interface by Michael Kerrisk (Chapter 41). Every concept is explained in plain language with real shell commands and C code examples.
Key Terms in This Chapter
Object File (.o) Static Library (.a) Shared Library (.so) ar command ld linker gcc -shared soname real name linker name -fPIC ldconfig LD_LIBRARY_PATH Position-Independent Code Dynamic Linker ldd
Static vs Shared Libraries at a Glance
| Feature |
Static Library (.a) |
Shared Library (.so) |
| Code is copied into binary? |
✅ Yes — at link time |
❌ No — loaded at run time |
| Disk space per program |
Larger (code duplicated) |
Smaller (shared on disk) |
| RAM usage |
Higher (each process has its own copy) |
Lower (one copy shared by all) |
| Update library without recompile? |
❌ No — must relink |
✅ Yes — just replace .so file |
| Suitable for embedded devices? |
Sometimes (no dynamic linker needed) |
Yes (saves space when many apps share libs) |
Chapter Topics
41.1 — Object Libraries: The Building Blocks
Understand what object files are, how the compiler and linker work together, and why grouping object files into libraries saves time and keeps projects clean.
Compilation pipeline Object files gcc vs ld -g debug flag Library types
41.2 — Static Libraries: Archives (.a files)
Learn how to create, update, and use static libraries with the ar command. Understand how the linker extracts only needed modules and how to link with -l and -L flags.
ar command Creating archives Listing contents -l and -L flags Selective linking
41.3 — Shared Libraries: Why They Exist
Discover the problems with static libraries and how shared libraries solve them. Learn about disk saving, RAM sharing, and library versioning concepts.
Advantages over static Disk & RAM saving soname Real name Linker name
41.4 — Creating a Shared Library
Step-by-step guide to building a shared library using gcc -fPIC -shared. Understand position-independent code and why it’s required for shared libraries.
-fPIC flag gcc -shared Position-Independent Code Versioned .so files
41.5 — Using Shared Libraries at Run Time
Learn how the dynamic linker finds shared libraries, how ldconfig works, and how to use LD_LIBRARY_PATH. Understand the ldd tool for inspecting dependencies.
ldconfig LD_LIBRARY_PATH ldd command /etc/ld.so.conf Dynamic linker
Interview Questions — Chapter 41
Comprehensive set of interview questions covering static vs shared libraries, PIC, versioning, ldconfig, soname, and common troubleshooting scenarios.
20+ Questions Detailed Answers Fresher & Senior Level