Mastering Linux Static and Dynamic Libraries: From ELF Basics to Real-World Usage
This guide walks through the ELF format, the compilation‑assembly‑linking pipeline, and step‑by‑step creation and usage of static (.a) and dynamic (.so) libraries on Linux, including gcc, ar, ldconfig, ldd and runtime loading with dlopen.
Linux programs are built from source code through compilation, assembly, and linking, producing ELF (Executable and Linking Format) binaries. ELF files can be executables, relocatable object files (*.o), or shared libraries (*.so). Understanding this pipeline is essential for effective Linux development.
Static Libraries (.a)
Static libraries are archives of object files. They are created with gcc -c to compile source files into .o objects and then packed with ar rcs libmytest.a st1.o st2.o. The resulting libmytest.a can be inspected with ar -t libmytest.a. When linking an executable, the linker copies needed code from the static library into the final binary, producing a larger executable that does not depend on external libraries at runtime.
Dynamic Libraries (.so)
Dynamic (shared) libraries contain code that is loaded at runtime. They are typically placed in /lib or /usr/lib. The system searches for them using ldconfig, which builds /etc/ld.so.cache from directories listed in /etc/ld.so.conf and /etc/ld.so.conf.d. If a new library is added, run ldconfig /path/to/dir to make it visible.
To build a shared library:
Compile source files to position‑independent objects: gcc -fPIC -c test_a.c -o test_a.o (repeat for other sources).
Link the objects into a shared object: gcc -shared -o libtest.so test_a.o test_b.o test_c.o.
Alternatively, a one‑step command can be used: gcc -fPIC -shared -o libtest.so test_a.c test_b.c test_c.c.
Using a Shared Library
Two common approaches exist:
Link‑time linking : compile the program with -L. -ltest so the linker resolves symbols against libtest.so. Ensure the library is in a directory searched by the loader or update /etc/ld.so.conf and run ldconfig.
Runtime loading : use the dlfcn.h API. Call dlopen("libtest.so", RTLD_NOW) (or RTLD_LAZY), retrieve symbols with dlsym(handle, "add"), and release with dlclose(handle). Compile with -rdynamic -ldl to enable this.
Key flags: -shared tells the linker to produce a shared object. -fPIC generates position‑independent code required for shared libraries.
By following these steps, developers can create, install, and use both static and dynamic libraries on Linux, gaining control over binary size, dependency management, and runtime flexibility.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
