Overview
Dynamic libraries (also called shared libraries) are collections of compiled code that are loaded into memory at runtime rather than compile time. They allow multiple programs to share the same library code in memory, reducing disk space and memory usage.Dynamic vs Static Libraries
Static Libraries (.a)
- Linked at compile time
- Code copied into executable
- Larger binary size
- No runtime dependencies
- Faster startup time
Dynamic Libraries (.so)
- Linked at runtime
- Code shared in memory
- Smaller binary size
- Requires library at runtime
- Updateable without recompiling
What is a Dynamic Library?
A dynamic library (.so file - Shared Object) contains compiled code that can be loaded into a program’s memory space at runtime. The .so extension is the Linux equivalent of Windows .dll files.
Advantages
- Memory efficiency: Multiple programs share one copy in memory
- Disk space savings: Library code not duplicated in each executable
- Easy updates: Update library without recompiling programs
- Plugin architecture: Load functionality dynamically
Disadvantages
- Runtime dependencies: Library must be present on the system
- Version conflicts: “DLL hell” if incompatible versions exist
- Slightly slower startup: Time needed to load and link at runtime
Creating a Dynamic Library
Library Interface
The header file defines the functions available in our library:main.h
Build Process
Compile with Position Independent Code
Compile source files with the What is -fPIC?Position Independent Code can be loaded at any memory address, which is essential for shared libraries that might be loaded at different addresses in different programs.
-fPIC flag:Automated Build Script
Create a script to automate the build process:1-create_dynamic_lib.sh
Using Dynamic Libraries
Method 1: Compile-Time Linking
-L.- Look for libraries in current directory-lmy- Link againstlibmy.so
Method 2: Runtime Library Path
Set the library path before running:Method 3: System Library Directory
Copy the library to a system directory:ldconfig updates the shared library cache.
Example Usage
Sample Program
0-main.c
Compilation and Execution
Examining Dynamic Libraries
List Library Dependencies
See what libraries an executable depends on:Display Symbol Table
-D flag shows dynamic symbols.
Check Exported Symbols
Environment Variables
LD_LIBRARY_PATH
LD_LIBRARY_PATH
Specifies additional directories to search for libraries:Use cases:
- Testing libraries before installation
- Using libraries in non-standard locations
- Development and debugging
LD_PRELOAD
LD_PRELOAD
Forces specific libraries to be loaded before others:Use cases:
- Function interception
- Debugging and profiling
- Overriding system functions
LD_DEBUG
LD_DEBUG
Enables dynamic linker debugging:Helps diagnose library loading issues.
Advanced Topics
Symbol Visibility
Control which symbols are exported:Versioning
Create versioned libraries:Loading Libraries Programmatically
Usedlopen() to load libraries at runtime:
Common Issues
error while loading shared libraries
error while loading shared libraries
Wrong ELF class error
Wrong ELF class error
Error message:Cause: Mixing 32-bit and 64-bit libraries/programsSolution: Ensure all components are compiled for the same architecture:
Undefined symbol errors
Undefined symbol errors
Cause: Library doesn’t contain required symbols or wrong library versionDebug:
Best Practices
Naming
Use
lib prefix and .so extension:libutils.solibmath.so
Versioning
Version your libraries:
libmy.so.1.0.0(full version)libmy.so.1(major version)libmy.so(development link)
ABI Stability
Maintain binary compatibility:
- Don’t change function signatures
- Don’t remove public symbols
- Use version scripts
Installation
Install to standard locations:
/usr/libfor system libraries/usr/local/libfor custom libraries- Run
ldconfigafter installation
Comparison with Static Libraries
| Feature | Static Library | Dynamic Library |
|---|---|---|
| Extension | .a | .so |
| Link time | Compile time | Runtime |
| Binary size | Larger | Smaller |
| Memory usage | Higher | Lower (shared) |
| Update process | Recompile programs | Replace library file |
| Dependencies | None | Library must be present |
| Portability | Better | Requires library on target system |
Related Topics
- Static Libraries - Compile-time linking alternative
- File I/O - Low-level file operations