Fundamentals 10 min read

Mastering Static and Dynamic Libraries on Linux: A Step‑by‑Step Guide

This article explains why reusable code should be packaged into libraries, compares static (.a) and dynamic (.so) libraries on Linux, and provides detailed commands, code examples, and troubleshooting steps for building and linking both types of libraries in C++ projects.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering Static and Dynamic Libraries on Linux: A Step‑by‑Step Guide

Why Use Libraries

Reusable code such as utility functions, algorithms, or timers is often duplicated across modules. Libraries package this code into binary files that the operating system can load, allowing multiple applications to share a single implementation.

Static and Dynamic Libraries on Linux

Linux supports two library formats:

Static library – file name ends with .a. The linker copies the object code from the library into the executable, producing a larger binary. Updating the library requires recompiling the executable.

Shared (dynamic) library – file name ends with .so. The library is loaded at runtime, so the executable stays small and multiple programs can share the same code. Updating the library does not require recompiling the dependent programs.

Example Project Layout

Assume the following directory structure:

sotest/
├─ lib/
│  ├─ b.h
│  └─ b.cpp
└─ btest.cpp

Header (b.h)

#include <stdint.h>
#include <string>
#include <iostream>
using namespace std;

const std::string &UserName();
const std::string &UserPwd();

Implementation (b.cpp)

#include "b.h"

std::string g_username = "mytest";
std::string g_userpwd  = "654321";

const std::string &UserName() { return g_username; }
const std::string &UserPwd()  { return g_userpwd; }

Building a Static Library

Compile the source file to an object file: g++ -c b.cpp -o b.o Create the static archive: ar rcs libb.a b.o Resulting file libb.a resides in sotest/lib.

Using the Static Library

Test program ( btest.cpp):

#include "lib/b.h"

int main(int argc, const char *argv[]) {
    cout << UserName() << ", " << UserPwd() << endl;
    return 0;
}

Compile and link against the static library:

g++ btest.cpp -L./lib -lb -o mytest
./mytest

Output:

mytest, 654321

Building a Dynamic Library

Compile with position‑independent code: g++ -fPIC -c b.cpp -o b.o Link the shared object:

g++ -shared -o libb.so b.o

Using the Dynamic Library

Compile the same test program, linking against the shared library:

g++ btest.cpp -L./lib -lb -o mytest
./mytest

If the runtime loader cannot locate libb.so, the program fails with “cannot open shared object file”. The loader searches a different path than the linker.

Resolving Runtime Loading Issues

Copy libb.so to a standard directory such as /usr/lib or /lib and refresh the cache: sudo ldconfig Add the library directory to LD_LIBRARY_PATH for the current session:

export LD_LIBRARY_PATH=$(pwd)/lib:$LD_LIBRARY_PATH

Persist the variable by appending the export command to ~/.bashrc (or /etc/bashrc for all users).

Alternatively, add the directory to /etc/ld.so.conf or create a file such as /etc/ld.so.conf.d/my.conf containing the path, then run sudo ldconfig.

Inspecting Linked Shared Libraries

Use ldd on the executable to list required shared libraries and their resolved locations: ldd mytest If a library cannot be found, ldd shows “not found” for that entry.

Summary

This guide demonstrates how to create and use both static ( .a) and dynamic ( .so) libraries on Linux, covering compilation commands, linking flags, and common runtime configuration steps such as library path management and cache refresh.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Compilationlinkingstatic libraryShared LibraryC++Dynamic Library
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.