C static libraries

Juan Camilo Cadavid Velásquez
4 min readFeb 28, 2021

In this blog, you will learn about C static libraries, we will take a look at why we should use them, how C static libraries work, how can we create them, and how we can use them.

So, why should we use libraries?

As a programmer you will find yourself using the same functions over and over again, you could create your functions inside of your source code and call them whenever you need them or you could do something more efficient and simple, in the long run, that thing I’m talking about is C static libraries. If we didn’t have a library set up the compiler would have to look for our functions inside our disk, and that would take a long time if our functions are scattered around our disk.

So basically a C static library is a file that acts as a toolbox, and your tools would be your functions, routines, and variables inside the library.

Okay, now we get why we should use libraries, but how do they work?

To understand how they work we need to take a look at the compilation process.

See the “Libraries” section and how it points to the Linker part of the compilation process?

The linker will connect the library to our executable file so that our program can execute our functions when needed. When compiling the library will be called to execute the program.

Now it’s time to learn how to create our own static libraries.

Let’s say you have a group of functions that you want to save in a static library, you will first need to turn your functions into object files, to do that we need to invoke the following command.

gcc -c your_function.c

The -c flag will give you object files after compilation.

Now that we have the object files of your function, it’s time to archive them, that’s another way of saying “creating the library”. For this, we will need to use the following command. Also, keep in mind that to create our library its name needs to start with “lib” and we need to add the “.a” extension.

ar rs libyourlibrary.a *.o

You will get the following output after invoking the previous command: ar creating libyourlibrary.a.

The final step to creating our library would be to index it, indexing will give a header to our library and this will make symbols easier to reference by the compiler, you’ll have to go through this step depending on your computer system. To index your library, run the following command.

ranlib libyourlibrary.a

If you want to view the contents of your library you’ll have to use this command.

ar -t libyourlibrary.a

Also, the “nm” command will show you the symbols in your library, you can run this command like this.

nm libyourlibrary.a

We just created our first C static library, yay! But there’s one last step to creating a library, we need a header file, so you can just use your favorite text editor and add the prototypes of your functions to your header file. Mine looks like this.

cat holberton.h

And finally, how do we use our library?.

You first need to compile your program and make it an object file so we can use our library, invoke the following command.

gcc -c your_program.c -o your_program.o

Now that we have the object file of our program (your_program.o) we have to link our library to our program, to do that invoke the following command.

gcc -o your_program your_program.o libyourlibrary.a

You can also link your library to your program by invoking this command.

gcc -o your_program -L . your_program.o -lyourlibrary

The “-L .” flag indicates that your library is located in the current directory, and the “-l” flag is to tell the compiler what the name of our library is.

The last two commands should leave you with an executable called “your_program”.

Et voilà ! you now know how to create C static libraries and how to use them. I hope you found this blog helpful and thank you so much for reading.

--

--