What, Why and How of the Static Libraries in C.

Alexis Oreiro
5 min readOct 12, 2020


Befor we start to talk about Static Libraries we need to know what is a library on C language.

A library in C is a collection of header files, exposed for use by other programs. The library therefore consists of an interface expressed in a (.h) file (named the "header") and an implementation expressed in a (.c) file. This (.c) file might be precompiled or otherwise inaccessible, or it might be available to the programmer. (Note: Libraries may call functions in other libraries such as the Standard C or math libraries to do various tasks.)

The format of a library varies with the operating system and compiler one is using. For example, in the Unix and Linux operating systems, a library consists of one or more objetct files, which consist of object code that is usually the output of a compiler (if the source language is C or something similar) or an assembler (if the source language is assembly language). These object files are then turned into a library in the form of an archive by the ar archiver (a program that takes files and stores them in a bigger file without regard to compression). The filename for the library usually starts with “lib” and ends with “.a”; e.g. the “libc.a” file contains the Standard C library and the “libm.a” the mathematics routines, which the linker would then link in.

Why use libraries in C?

As a programmer, you may find yourself using the same function or functions repeatedly. In this case, it is best to put this function or functions in a library to speed up the compilation of the program. C libraries store files in object code; during the linking phase of the compilation process files in object code are accessed and used. It is faster to link a function from a C library than to link object files from a separate memory sticks or discs.

Two types of libraries in C: Static and dynamic

There are two types of libraries in C static and dynamic. Dynamic libraries are shared libraries with specific functions launched during the execution of a program and contribute to “reduced memory consumption”(techopedia.com). Dynamic libraries are linked in two stages. Static libraries produce object files and standalone executable files (wikipedia.org). These libraries can be linked to a program without recompiling the code. Static linking or the linking of static libraries creates larger files because of the creation of the standalone (executable file) files.

How do static libraries work?

Static libraries are added during the linker phase of the compilation process (above). During the linker phase: the linker links access all the libraries to link functions to the program. Static libraries are merged with other static libraries to create an executable program. During the compilation of a program, the static library is called to execute the program.

How to create static libraries

To create a static library you have to use the ‘ar’ or archiver program. The idea is that these functions are being archived until needed. A function saved in .c format is recompiled into an object file, .o .

Creating a Static Library step by step:

Step 1.

Create all your source files. Source files hold any functions you will use.

Step 2.

Compile the source files into object files. Using GCC use this command:

$ gcc -c *.c

This will change the object files to source files.

Step 3.

Create a static library. Using “libholberton” as an example of a library name, this command creates a Static library.

$ ar -rc libholberton.a *.o

The ‘ar’ is the program being used to archive the files. The ‘c’ tells the program to create a library. The ‘r’ tells the program the replace or update older files in the library.
With step three a static library has been created.

If needed use the ‘ranlib <libraryname.a> to index the library.

$ ranlib libholberton.a

Indexing a library will let the compiler know just how old a library file is. Indexing also gives the library file a header and makes it easier for the compiler to reference the symbols. This step may or may not be necessary depending on your computer system.

To view the contents of the static library access it using: ‘ar -t libholberton.a’ in the command line.

The ‘nm’ command will let you see the symbols in your library; nm lib_test.a.

To use the static library call on the library during the linking phase of program compilation.

gcc main.c -L. -lholberton -o main

‘-L’ specifies the library path.
‘-l’ goes before the library name.

You are able to run the executable program.


How are static libraries used?

We have created a static library and can now use it.

Step 1.

Write a program:

#include "holberton.h"int main(void)
_puts("\"My Static Library is working.\"");
return (0);

In the header: “holberton.h” contains function and function definitions used to tell the compiler how to call functionality. It contains “data types and constants used with the libraries”(geekforgeeks). When we compile the file we call the library:

$ gcc main.c -L. -lholberton -o quote

Now we can run the executable:

$ ./quote

If the library is working properly the output should be:

"My Static Library is working."

We have created a functioning static library.

I hope this article helped you become more than familiar with static libraries and C libraries in general. They are very useful tools in programming in C and C++. Since the beginning of C programming, there was a need for libraries, a place to store the commonly used functions in C for easy access and use by programmers using C. Creating a library tailored to your personal programming needs may save you a lot of time during compilation and programming. We have discussed how static libraries work, and in encourage you to investigate how dynamic libraries may be of use to you as well.

Below there is a video about static libraries.



Alexis Oreiro

Working to succeed in Cyber Security.