Static Libraries in C

Skander Amireche
2 min readMar 1, 2020

Libraries offer services that level the intellectual playing field. That means that they allow people of any income level or background to access high-quality information knowledge, to use computers, or to use what they want. … Libraries are spaces where people of all ages can follow lifelong learning.

A static library or statically-linked library is a set of routines, external functions, and variables that are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable.

How to create a Static Library file

Step 1

Create a static library using Gcc, we need to compile the library code into a target file

$ gcc -c *.c

step 2

all files that had an extension .o including the initial creation of the library

$ ar rcs libholberton.a *.o

r Replace the files if they already existed.

c Create the files if it does not exist.

s Build an index of content.

step 3

check all your obtained files with ar by using this command:

$ ar -t libholberton.a

A static library is purely a collection of .o files put together in an archive that's something like a zip file (with no compression). When you use it for linking, the linker will search the library for .o files that provide any of the missing symbols in the main program, and pull in those .o files for linking, as if they had been included on the command line like .o files in your main program. This process is applied recursively, so if any of the .o files pulled in from the library have unresolved symbols, the library is searched again for other .o files that provide the definitions.

Keep going and have fun ^_^

--

--