Static Libraries in C: the shoulders of giants

Ric Hincapie
4 min readMar 1, 2020

All you need to know about Static Libraries in C.

Index: What is a library in C and what does it contains — Why are C libraries Giants’ shoulders to step on? — How a C library works?–

Once, Isaac Newton said: “if I have seen further it is by standing on the shoulders of Giants”, using the phrase coined by Bernard de Chartres. It means that the only reason why we can see further away than our predecesors is not because of we being more clever or sharper than them, but because we step on the “knowledge building” we inherit from their efforts.

What is a library in C and what does it contains?

A library is an archive enclosing object files, which contain object code, and have an “.a” file extension. Object code is the product of a compiler, specifically the result of the Assambler stage, where the code is translated into “machine language” for the hardware to execute the user’s instructions. Libraries arrange and index in one file functions, variables and other usefull code for us to use.

Why are C libraries Giants’ shoulders to step on?

C Libraries are very usefull for a wide range of reasons. The very first of them is because they work and you can rely on them. But work for what? Well… C language has 29 different libraries packed with functions of all kinds, from simple output functions, to complex math and memory manipulation ones. This means that you can use the work of different and talented groups of programmers who maintain and improve those libraries to achieve your particular objectives. If you have written your own functions, you can also arrage them in a library and use them easily. Besides, C libraries are portable, so you can use them and expect the same perfomance in different computers, making the programs you write using them portable too.

All this comes to time saving characteristics. What can I say about performance? As C is a compiler based program language, which means it needs to be compiled down during runtime in order to deliver machine language and execute commands, libraries save a lot of processing resources since the object files are already in machine language almost ready to be executed, laking only the linking phase of the compilation.

How a C library works?

A C library works by arranging and indexing a group of functions in object code status (machine language) inside an archive type of file. Inside it has an index file that’s mandatory to be updated, and allows the program using the library to easily indentify and find the object files.

C libraries use a header file, whose extension is “.h”, as an interface expressed file to be called from within the user’s program. It informs the program how to call the functions and their basic characteristics through function prototypes. At the begining of any program, the header file must be called in this way for built-in libraries:

#include <my_built-in_library>

or like this for user’s local libraries:

#include "user_created_library"

How to create and use a C static library?

That’s an easy task! Follow this steps and you will be done really quick.

1 — Create a header file in the same directory where you put the functions prototypes in different lines

void Sum( int a, int b );
int MyFunction( int n);

Keep in mind that a header file has it own structure.

2 — Compile all your .c function files like this:

gcc -c filename1.c filename2.c filename...n.c

Please remember that all the .c files must be in the current directory. The -c flag is asking the compiler to output only the object code file (“.o”).

3 — Use the command ar (for “archive”) to take all the object code files into one single file

ar rc -s my_library_name.a my_.o_file1 my_.o_file2 my_.o_file...n

In this command you ask ar to: “r” insert the files members into the archive; “c” create the archive; and “-s” create or update the index file inside you library (which is very important to do).

You have created you own library! Congrats!

4 — Now it is time to add the library’s name to the list of object file names given to the linker to work with. This is a delicate step. You need to inform the linker where your library is with the “-L” flag, then give it the path to the header file with the “-I” flag, and finally tell it what file to include with the “-l” flag. Here you can see an example:

gcc -I /path/to/new_library/header -L /path/to/new_library 
-lname_of_lib_without_prefix_lib_and_without_.a_

Note that when using the “-l” flag the compiler will automatically add the prefix “lib” and the sufix “.a”.

There’s a different kind of library called shared library. You can have further information here.

So, now you can climb up to the Giant’s shoulder and use all their power to tackle your programming needs. Or you can have your own customizable library to fit your needs.

Thanks for reading! :D

--

--