Atenea Castillo
4 min readOct 12, 2020


What about C libraries…

From: Connie Hanzhang Jin/NPR

When you hear the word library what problably comes to your mind is a big and charming place where books are stored and available for you to read. If you take that image, but instead of books, you think in files or programs and a computer you will realize that they need to be stored somewhere, let’s say: a library, because, in any case, a library is a collection of sources of information.

Getting more specific, in C language, during the process of compilation, as I explain in a previous post, in order to make a file executable, the compilator goes throuhg some stages and at some point it has to look into C libraries to find the instructions described in a program. In C, a library is a file (with .a extension) or collection of object files (created by others or by you) that contains variables and functions to use in a program, this enables you to perform different things such as take input from the keyboard, handle with numbers or create math operations, therefore libraries are clasified according to the the type of action their functions do just as books libraries are clasified by topics; C has more than 25 “topics”.

Maybe you think: ok, but why using libraries? Well, imagine if every time you go to a library looking for some books you have to check each shelve because there is not a clasification by topic, that would be crazy, huh? Or when writing a program with specific instructions you need to create a fuction for everything o call every library, that would be kind of boring and a waste of time; instead, you can just call the library with the “topic” you need and voilá: you have more time for coding ;).

A C library indexes a group of functions in object code (machine language) inside an archive. This index has to be updated, and allows the program using the library to easily indentify and find those 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 for built-in libraries or your own libraries.

In C there are two big kinds: static and dinamic. I will focus on the first one. A static library is a collection of files linked to a program when linking phase happens (remember that the compilation process has 4 phases: preprocessing, compiling, assembling and linking) Here, the process looks for the libraries where the functions inside the code are located, so object code of the program is combined with object code of the selected libraries. For example, if you use a printf function, the linker will look for it in the stdio.h library, if you didn’t specified that library your computer will explode! Nah (haha?), the program just won’t run. Another thing it does is to verify that only one library contains the main function.The output of the linker is an executable file and when run it you will have your result.

How to create one then?

In order to create a static library, first you create a header file with .h extension where function prototypes are kept; then it is necesary to use a program called “archiver” or “ar”, that allows you to do more things like modify and list the libraries. Another thing needed is to convert .c files into object files, this is is easy to get using -c flag like this:

gcc -c file.c file1.coutput: file.o fileq.o

The syntax to create a library is as follows:

ar -rc lib_name_of_library.a file.o file1.o

At this point you can index you library, this means to speed up symbol-lookup inside the library, and to make sure that the order of the symbols in the library won’t matter during compilation.

Next, it is importat to tell the linker where the library is located, for this you can use a -L flag; then it is also important to tell what file will be included using the -l flag (-l flag will add lib and .a to the name of the library). Here is an example:

gcc -L /path/to/library -lname_of_library

Here is a complete example of the process worked in my pinky terminal, so you don’t feel so lost in this sea of technical exaplanation:

Turn .c files into .o files
Creating a library

And that’s all folks!

By the way, I still prefer to go to a real library and smell some old books, like we did in the good old days.