0Pricing
C Academy · Lesson

Static and Shared Libraries

Build and link libraries.

Static and Shared Libraries is a free C Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What A Library Is

A library is a bundle of compiled object files you can reuse across programs without recompiling the source each time.

C has two kinds:

  • Static libraries (.a) copied into the executable at link time
  • Shared libraries (.so) loaded at run time

Building Object Files

Both kinds start from object files. Compile your sources with -c as usual.

These .o files are the raw material the archiver or linker will package.

gcc -c mathlib.c -o mathlib.o
gcc -c strlib.c -o strlib.o

Creating A Static Library

The ar archiver bundles object files into a .a archive. The conventional name is lib<name>.a.

The flags mean: r insert, c create if needed, s write an index.

ar rcs libmymath.a mathlib.o strlib.o

Linking A Static Library

Link with -L for the search directory and -l for the library name (drop the lib prefix and .a suffix).

The needed code is copied into the executable, which then runs standalone.

gcc main.c -L. -lmymath -o app

Static Tradeoffs

Static linking is simple and self-contained:

  • The executable carries everything; no runtime dependency
  • But each program holding a copy means larger binaries
  • Fixing a library bug requires relinking every program that used it

Position-Independent Code

Shared libraries can load at any memory address, so their code must be position independent. Compile with -fPIC.

This generates code that uses relative addressing instead of fixed addresses.

gcc -fPIC -c mathlib.c -o mathlib.o
gcc -fPIC -c strlib.c -o strlib.o

Creating A Shared Library

Build the .so with -shared. The convention is lib<name>.so.

Unlike a static archive, this is a real linked object the loader can map at run time.

gcc -shared mathlib.o strlib.o -o libmymath.so

Linking A Shared Library

Linking looks identical to the static case. The difference is that the code is not copied in; only a reference is recorded.

The executable stays small but now depends on finding libmymath.so at run time.

gcc main.c -L. -lmymath -o app

Finding The Library At Runtime

At startup the dynamic loader searches standard paths for the .so. A library in the current directory is not on that list.

Point the loader at it with LD_LIBRARY_PATH (Linux) for testing.

LD_LIBRARY_PATH=. ./app

Shared Tradeoffs

Shared libraries trade simplicity for flexibility:

  • One copy in memory is shared by many programs, saving RAM and disk
  • A bug fix in the .so benefits every program without relinking
  • But the program fails to start if the .so is missing or incompatible

This last risk is the classic 'dependency hell'.

Inspecting Dependencies

Two tools help you see what a binary links against:

  • ldd app lists the shared libraries the loader will need at run time
  • nm libmymath.a lists the symbols (functions) inside a library

If ldd shows 'not found', the loader cannot locate that .so on its search path.

ldd app
nm libmymath.a

Quick Check

Match the flag to the goal.

Recap

You can now build and link both library kinds:

  • Static: ar rcs libX.a *.o, code copied into the binary, self-contained but larger
  • Shared: compile with -fPIC, build with -shared, code loaded at run time
  • Link both with -L and -l; shared libs need a runtime search path like LD_LIBRARY_PATH

You now have the full preprocessor-to-build toolchain.

Frequently asked questions

Is the “Static and Shared Libraries” lesson free?

Yes — the full text of “Static and Shared Libraries” is free to read here on the web, and the C Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.

What will I learn in “Static and Shared Libraries”?

Build and link libraries. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Static and Shared Libraries” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Conditional Compilation
  2. Multi-File Projects
  3. Makefiles
  4. Static and Shared Libraries
← Back to C Academy