Fundamentals 6 min read

How to Build and Link a Shared Library (.so) on Linux with GCC

This guide explains the naming conventions, source code, compilation commands, and linking techniques for creating a Linux shared library (.so) using GCC, including options like -fpic, -shared, absolute‑path linking, and the -l flag after copying the library to /usr/lib.

ITPUB
ITPUB
ITPUB
How to Build and Link a Shared Library (.so) on Linux with GCC

Background

Based on Chapter 5 of C Expert Programming , the article demonstrates how to build and link a shared library on Linux.

File Naming Conventions

Shared libraries use the .so extension and are named libname.so (optionally with version numbers). Static libraries use libname.a. The transitional libname.sa is rarely used.

Environment

Operating system: Debian GNU/Linux 2.6.21‑2‑686.

GCC version: 4.1.3.

Library Source (myfunction.c)

/* Author: Godbach */
/* E‑mail: [email protected] */
#include <stdio.h>

int my_lib_function(void)
{
    printf("Library routine called from libmyfunction.so!
");
    return 0;
}

Compiling the Shared Library

From the directory /home/program/ run:

gcc -fPIC -shared -o libmyfunction.so myfunction.c

The -fPIC option generates position‑independent code required for shared objects. The -shared flag tells GCC to produce a shared library rather than an executable. It is advisable to compile as a regular user; use sudo only when installing the library system‑wide.

Test Program (test.c)

#include <stdio.h>

int main(void)
{
    my_lib_function();
    return 0;
}

Linking and Running with an Absolute Path

Compile the test program by specifying the full path to the shared library:

gcc -o test test.c /home/program/libmyfunction.so
./test

Output:

Library routine called from libmyfunction.so!

Linking via System Library Path

To avoid an absolute path, copy the library to a standard directory such as /usr/lib/ and refresh the linker cache (optional):

sudo cp libmyfunction.so /usr/lib/
sudo ldconfig   # refresh the cache
gcc -o test test.c -lmyfunction
./test

The -lmyfunction option directs the linker to search for libmyfunction.so (or libmyfunction.a) in the library search paths, omitting the lib prefix and file extension. Ensure that the directory containing the library is listed in /etc/ld.so.conf or referenced via the LD_LIBRARY_PATH environment variable.

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.

CompilationLinuxdynamic linkingShared Librarygcc
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

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.