Static Libraries in C

Jorge Rodríguez
3 min readOct 13, 2020

While you are learning how to code you have a lot of things that you use, but sometimes you don’t really know how they work, in this case, I would like you to read this blog and get a little more information on how static libraries work.

But first… What is a library?

In high level programming a library is a collection of functions. This is very helpfull so you do not need to re-write the whole code of the function to your program. By only adding the library at the top of your code, and calling the functions you need inside of your program.

How do they work?

This process will call the function from the library, complete the function process and return to your program, without having to type the whole function into your final code of the program.

The most important thing to keep in mind while working with the static libraries is that the more functions you store in the library, the size of the library file is going to increase. Since the compiled functions are stored in there, we are going to get into details below.

Step by step

Now let’s see the whole process involved on creating a static library in C:

  1. We need to create the functions in *.c format files using any editor you would like.
  2. Once all the functions are created, we need to change their format to *.o. To this end we need to stop the compilation process on the compiler part (You can check the whole compilation procedure at my previous blog here), by using the comand gcc -c and the file_name.c.
  3. Then we need to take the *.o files and add them to the library. This process is done by using the comands ar rc library_name.a file_name.o other_files.o.

The result of the process is an *.a file, since all the functions in the format *.o where involved during this process, the size of the library is going to increase based on the length of the code within the functions.

However with this type of library you can move the file to other position and as long as any change has to be performed it is going to work perfectly.

I hope that once you have read this the process of creating a library in C and the usage is even more clear to you now.

--

--

Jorge Rodríguez
0 Followers

I´m a full stack student at Holberton School, I'll write about technical genre!