Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.
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
Tutorial Pages
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.
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.
# 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
Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.
