Create a Static Library With ar and Link Against It
Build a static library from object files with ar rcs, link a program against it with -L and -l, and see how the linker pulls in only the members it needs.

A static library is the simplest way to share code between programs. You compile a set of source files to object files, bundle them into one archive, and tell the linker to take what it needs from the archive when it builds a program. There is no run-time component: the code you use is copied into the executable, and the library file is not needed to run it. This post builds a small library from scratch with ar, links a program against it, and then shows the two behaviors that surprise beginners: the order of arguments on the link line, and the fact that the linker copies only the archive members it needs. It uses GCC 14 and GNU binutils 2.42 on Linux. The archive tool is documented in the ar manual. For a comparison with shared libraries, see the complete guide to static and shared libraries in C.
A library with two members
Make a directory with a header and two source files. One module does arithmetic and the other formats text:
#ifndef MATHX_H
#define MATHX_H
int mathx_add(int a, int b);
int mathx_square(int a);
#endif#include "mathx.h"
int mathx_add(int a, int b)
{
return a + b;
}
int mathx_square(int a)
{
return a * a;
}#ifndef GREET_H
#define GREET_H
void greet_print(const char *name);
#endif#include <stdio.h>
#include "greet.h"
void greet_print(const char *name)
{
printf("hello, %s\n", name);
}Compile each file to an object, without linking. The -c flag stops after assembling:
gcc -O2 -Wall -c mathx.c greet.c
ls *.ogreet.o mathx.oPacking them with ar
The ar command creates and manages archives. The common invocation is rcs, three flags in one word: r inserts the files, replacing existing members of the same name, c creates the archive without a warning if it does not exist, and s writes the symbol index that the linker uses to find members quickly. By convention a library is named lib followed by a name and .a:
ar rcs libsmall.a mathx.o greet.o
ar t libsmall.amathx.o
greet.oar t lists the members. The symbol index is inside the archive, and you can inspect it with nm -s libsmall.a. That index is what makes archives fast to search. An archive without one, for example after a tool rewrote it, produces the error archive has no index; run ranlib to add one. The cure is ranlib libsmall.a, or building with the s flag as above.
Linking against the library
Write a program that uses the arithmetic module:
#include <stdio.h>
#include "mathx.h"
int main(void)
{
printf("%d\n", mathx_add(mathx_square(3), 4));
return 0;
}There are two ways to name the library. You can give the file directly:
gcc main.c libsmall.a -o demo
./demo13Or you can use the search form, which is the usual way: -L adds a directory to the search path and -l names the library without the lib prefix and the .a suffix:
gcc main.c -L. -lsmall -o demo
./demo13Both produce the same program. -lsmall makes the linker look for libsmall.so first, and libsmall.a if there is no shared version in the same directory. To insist on the static one when both exist, pass the file name directly or add -static, which makes the whole link static.
Order matters
The linker processes arguments left to right, and for each archive it extracts only the members that satisfy symbols already undefined. A library placed before the code that needs it satisfies nothing, and the link fails:
gcc -L. -lsmall main.c -o demo/usr/bin/ld: /tmp/ccXXXXXX.o: in function `main':
main.c:(.text+0xc): undefined reference to `mathx_square'
/usr/bin/ld: main.c:(.text+0x1c): undefined reference to `mathx_add'
collect2: error: ld returned 1 exit statusWhen -lsmall is read, no symbol is yet undefined, so the linker takes nothing from the archive. Then main.c introduces two undefined references and no library follows to satisfy them. The rule is: objects first, libraries after, and a library that depends on another goes before it. The temporary file name in the message differs from run to run.
Only the needed members are linked
The archive holds two members, but the program uses one. The linker took mathx.o and left greet.o behind, which you can confirm:
nm demo | grep -E 'mathx|greet'0000000000001149 T mathx_add
0000000000001161 T mathx_squareNo greet_print appears. That selective extraction is the point of an archive over a pile of object files: the executable contains only what it uses. It works at the member level, so the granularity is the object file. If you put twenty functions in one .c file and use one, you link all twenty, unless you also compile with -ffunction-sections and link with -Wl,--gc-sections, which lets the linker discard unused functions inside a member.
The consequence for library design is that small, focused source files make better static libraries. Putting each function in its own file is the classic approach in libraries such as the C library, so a program pays only for what it calls.
Updating and inspecting
Replace a member after changing a source file with the same r flag, and delete one with d:
gcc -O2 -c mathx.c
ar rs libsmall.a mathx.o
ar d libsmall.a greet.o
ar t libsmall.amathx.oExtract a member to the current directory with ar x libsmall.a mathx.o. To see what the library offers without reading source, use nm:
nm --defined-only libsmall.amathx.o:
0000000000000000 T mathx_add
0000000000000010 T mathx_squareThe addresses are offsets inside each member and are an example.
Building it with make
A Makefile rule makes the archive a normal build target, so changing a source file rebuilds the member and the library:
CC := gcc
CFLAGS := -O2 -Wall
libsmall.a: mathx.o greet.o
ar rcs $@ $^
demo: main.o libsmall.a
$(CC) main.o -L. -lsmall -o $@$@ is the target and $^ the list of prerequisites. Because demo depends on libsmall.a, editing mathx.c rebuilds the object, the archive and the program, in that order. A common mistake is to omit libsmall.a from the prerequisites, which means make will not relink after the library changes.
When a static library is not the right choice
A static library copies code into every program, so a bug fix needs every program rebuilt. If many programs share a large library, a shared object saves disk and lets you fix once. Static linking wins when you want a single self-contained file, when the library is small, or when you cannot rely on the target having the library installed. Licensing also differs: statically linking a library under the LGPL carries conditions that dynamic linking relaxes, so read the license before you ship.
Header files and where they live
The archive holds code only. The compiler needs the header to know the function signatures, so a library you share is really two things: libsmall.a and its headers. Keep the headers in an include directory beside a lib directory, and compile users with -Iinclude to find them and -Llib to find the archive. A tidy layout is include/mathx.h, include/greet.h and lib/libsmall.a. Install both with a single make install rule so that nobody ships one without the other, and write headers that are safe to include twice with guards like the MATHX_H guard above. When you change a function's signature, change the header and rebuild every user of it: an archive does not check that a caller agrees with the definition, because C object files carry no type information, and a mismatch is a run-time bug and not a link error.


