Intermediate · The preprocessor, multi-file programs and make · topic 40 of 60 · 25 min
Libraries: static and shared
Explanation
Header plus library equals an API
To use someone else's C code you need two things: the header (.h) with the declarations, so your code compiles, and the library with the compiled machine code, so it links. The standard C library (printf, malloc, strlen) is linked automatically; others must be named on the command line.
#include <math.h>
double r = sqrt(2.0);Linker flags
| Flag | Meaning | Example |
|---|---|---|
| -I dir | Also search dir for #include files | -I include |
| -L dir | Also search dir for libraries | -L lib |
| -lname | Link libname.a or libname.so | -lm, -lpthread, -lsqlite3 |
gcc -std=c17 -Wall -I include src/app.c -L lib -lgst -o appMaking a static library
gcc -std=c17 -Wall -c tax.c money.c # → tax.o money.o
ar rcs libbilling.a tax.o money.o # archive them into a static library
gcc main.c -L. -lbilling -o app # link: the needed code is COPIED into appMaking a shared library (Linux)
gcc -std=c17 -Wall -fPIC -c tax.c money.c # position-independent code
gcc -shared -o libbilling.so tax.o money.o # build the shared library
gcc main.c -L. -lbilling -o app # link against it
LD_LIBRARY_PATH=. ./app # tell the loader where to find it at run time| Static (.a / .lib) | Shared (.so / .dll / .dylib) | |
|---|---|---|
| When code is attached | At link time, copied into the executable | At run time, loaded by the system |
| Executable size | Bigger | Smaller |
| Updating the library | Relink every program | Replace one file (if compatible) |
| Deployment | One self-contained file | Library must be present on the machine |
| Memory with many programs | Each has its own copy | One copy shared in memory |
Libraries you will meet
- libm (math), libpthread (threads; part of glibc in recent versions), libdl (dynamic loading).
- SQLite, zlib (compression), OpenSSL (cryptography), libcurl (HTTP), cJSON (JSON).
- On embedded systems: vendor HALs (hardware abstraction layers) and RTOS kernels such as FreeRTOS.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Libraries: static and shared · 1/6
In this lesson you will use and build C libraries.
Prefer a video by a teacher? Find free videos on “c static library vs shared library ar gcc -shared fpic linking” on YouTube ↗
In real life: Using SQLite in a C program
A small inventory tool for a hardware shop stores its data in SQLite. The developer installs the SQLite development package (sudo apt install libsqlite3-dev), includes <sqlite3.h> and links with -lsqlite3. The tool then reads and writes a single database file with SQL.
#include <sqlite3.h>
#include <stdio.h>
int main(void) {
sqlite3 *db;
if (sqlite3_open("shop.db", &db) != SQLITE_OK) { fprintf(stderr, "%s\n", sqlite3_errmsg(db)); return 1; }
sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS item(name TEXT, qty INTEGER);", NULL, NULL, NULL);
sqlite3_close(db);
return 0;
}
/* gcc shop.c -o shop -lsqlite3 */Practice (0/3 done)
The program builds but at start says "error while loading shared libraries: libgst.so: cannot open shared object file". What is wrong?
Which command creates a static library from tax.o and money.o?
Turn your money module into both a static library (libmoney.a) and a shared library (libmoney.so). Link one program against each, compare the executable sizes with ls -l, run the shared version (setting LD_LIBRARY_PATH if needed), and use ldd to see which libraries each executable needs.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Conditional compilation and include guardsThe preprocessor, multi-file programs and make · same module
- What C++ is and how compiling worksC++ Programming (modern C++ and DSA) · First steps: compiling, output, variables and input
- Cross-compiling and build flagsGo Programming (backend and cloud) · CLI tools and Go in cloud and DevOps
- static members and finalJava Programming (Core to Spring Boot) · Classes and objects
- What C# and .NET areC# and .NET · Getting started with C# and .NET
- Access modifiers, static members and encapsulationC# and .NET · Classes and objects
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
Which flag links the math library on Linux?
Answer: -lm
The library is libm.
When is the code of a static library added to a program?
Answer: At link time
It is copied into the executable.
What is the main advantage of shared libraries?
Answer: One copy shared by many programs, and updates without relinking
Smaller executables and shared memory.
What does -I include do?
Answer: Adds the include folder to the header search path
For finding .h files.
What does extern int count; in a header mean?Related
Answer: count is defined in some .c file; this just declares it
A declaration without storage.
Which flag asks the compiler to optimise the program for speed?Related
Answer: -O2
-O2 (capital letter O) turns on optimisation. Lowercase -o names the output file.
Does cross-compiling prove your program works on the target system?Related
Answer: No, you still need to run and test it there
Building and running are different things.
Why is main declared static?Related
Answer: So the JVM can call it without creating an object
No object exists when the program starts.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- This is the last topic of “The preprocessor, multi-file programs and make”. The module exam comes next.
- Module 8 exam: Preprocessor, multi-file programs and make · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Bits, undefined behaviour and safety. 60 topics left in the course before the final exam.