Master Linux Static and Shared Libraries: Build, Link, and Load
This guide explains Linux library types, naming conventions, and step‑by‑step commands for creating static (.a) and shared (.so) libraries, managing symbolic links, checking dependencies, runtime search paths, dynamic loading, and using GNU Libtool.
Libraries greatly improve C/C++ code reuse, but beginners often find them challenging; this article provides a concise Linux‑focused overview of static libraries, shared libraries, and runtime‑loaded libraries.
Linux Library Types
Linux supports two main library formats:
Static library (.a) : linked directly into the executable at build time.
Shared library (.so) : can be linked at build time (visible to the linker) or loaded dynamically at runtime using explicit loading functions.
Naming Convention
Libraries must start with lib but the -l linker flag omits the prefix and suffix. Example: gcc src-file.c -lm -lpthread This links libm.a and libpthread.a.
Creating a Static Library (.a)
Compile object files, e.g., cc -Wall -c ctest1.c ctest2.c (produces ctest1.o and ctest2.o).
Create the archive: ar -cvq libctest.a ctest1.o ctest2.o.
Optionally list contents: ar -t libctest.a.
Generate a symbol table (often integrated): ranlib libctest.a.
The .a format corresponds to Windows .lib files.
Creating a Shared Library (.so)
Compile objects with position‑independent code: gcc -Wall -fPIC -c *.c.
Link with -shared and set the soname: gcc -shared -Wl,-soname,libctest.so.1 -o libctest.so.1.0 *.o.
Create convenient symlinks:
mv libctest.so.1.0 /opt/lib
ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.so.1
ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.soThe libctest.so link is used at compile time ( -lctest), while libctest.so.1 is the runtime version.
Link an application against the shared library:
gcc -Wall -I/path/to/include-files -L/path/to/libraries prog.c -lctest -o progInspecting Dependencies
Use ldd prog to list required shared libraries, e.g.:
ldd prog
libctest.so.1 => /opt/lib/libctest.so.1 (0x00002aaaaaaac000)
libc.so.6 => /lib64/tls/libc.so.6 (0x0000003aa4e00000)
/lib64/ld-linux-x86-64.so.2 (0x0000003aa4c00000)Object File Formats
Object files differ across systems: Windows uses PE, Linux/Unix use ELF, macOS uses Mach‑O, AIX uses XCOFF. Tools such as nm, objdump, and readelf can examine symbols.
Runtime Library Search Path
The system locates shared libraries via /etc/ld.so.conf and the cache updated by ldconfig. Environment variables like LD_LIBRARY_PATH (Linux/Solaris), LIBPATH (AIX), or DYLD_LIBRARY_PATH (macOS) also affect the search.
Dynamic Loading and Unloading
For plugin‑style architectures, libraries can be loaded at runtime using dlopen, dlsym, and dlclose from dlfcn.h. Executables that use this mechanism should be linked with -rdynamic to export all symbols to the dynamic symbol table.
GNU Libtool
Many projects use libtool, a wrapper script that simplifies building portable libraries. It generates .la files that reference the underlying .a or .so and contain version information.
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.
