Libraries in C

Esteban Castaño Zapata
4 min readOct 12, 2020

To better explain libraries, I would like first to share a personal experience. I started mechanical engineering to learn about robotics, a dream i had since watching Ghost in the Shell. Unfortunately, the university I was studying decided to kill the program and so my hopes to learn about the subject for the time being. But years later an interesting gadget appear, called Arduino. In the old days, you could program a microcontroller using something we locally call a burner, but at the time for a newbie it was difficult to understand if something wasn’t going well and why, especially if you don’t have the appropriate hardware. Enter the Arduino, a single board microcontroller that you can clearly and easily connect to your computer, programming it and run it with several inputs and outputs. This was the beginning of my path to achieve that early dream.

With Arduino I got my first encounter with libraries. In one particular case, I wanted to control a step motor, which you can do in several ways, but following a tutorial on the subject, I find a library called stepper.h. This library gives you access to functions such as the speed and the steps of the motor. In the case of Arduino, you can find many other libraries for different purposes, like controlling displays, sd cards, network connections, etc.

So in a not so formal definition, a library allows you to use functions that someone else created so you don’t have to do everything from zero.

In the context of the C language, a library contain several header files and each header file contains functions declarations that can be share between many source files. Normally, the process to use in a program a function that it’s declare in the header file it’s the following:

First you create a file with the function. Lets say the function it’s stored in a file called add.c, a function that adds two numbers called add().

Second, you create a header file, called MyHeader.h, that includes the prototype of the functions we want to use. In this case the prototype its

int add(int a, int b);

Third, in the main program where you want to use the add() function (in this example, the file that store the main program it’s called test.c) , you called the header to link the files involve. In this case you do this by writing at the head of the program #include “Myheader.h”.

Finally, at the moment the program its compiled, all the files included in the process are needed, something like this

Gcc add.c test.c –o sum

In essence, executing the program sum will add to numbers using the function adding found in the file add.c, that its link to the main file test.c by means of the header Myheader.

Now imagine that we have many functions in the header that we want to use, in this case we will need a copy of every file that it’s linked for the compilation, which it’s kind of cumbersome and not something that can be share easily.

That’s when the concept of library shows up to help us. The library can contain all the files with the functions required, and only the library is needed to make the linking process during the compilation. So following in the previous example and create a library , let’s say we also want to use more functions that are in several more files (subs(), mult(), div()), files like this

subs.c mult.c div.c

First we include them in the header file. We will have a header that look like this:

#ifndef MYHEADER_H

#define MYHEADER_H

int add(int a, int b);

int subs(int a, int b);

int mult(int a, int b);

int div(int a, int b);

#endif

The next process is to compile the files without linking them. This can be accomplished using the option –c of the compiler gcc.

gcc add.c subs.c mult.c div.c –c

This stage creates files that have the same name of the original files but with the .o extension.

Now we create the library ops using the command ar

ar rc libops.a add.o subs.o mult.o div.o

The flag r allows to replace older files with new ones and the flag c creates the library if it doesn’t exist already.

The next part it’s to indexed the library. This helps to speed up the searching process for the compiler. To index a library, the command ranlib is used. In our example it would be like

ranlib libops.a

now using compiling the file test.c that uses all the functions only needs the header Myheader.h and to compilate we only need the library as follows:

gcc test.c -L -lops -o tops

this create an executable name tops that includes all the necessary files and functions.

This shows the advantages that libraries have, such as portability (you can include all the information in one place and share it) and speed, using the indexing capability.

--

--

Esteban Castaño Zapata
0 Followers

Aspiring software engineer at Holberton School