What are C static libraries?

Andres Aristizabal
4 min readOct 12, 2020

Before starting with static libraries in C, it is important to know and understand from a broader point of view the concepts of libraries in programming.

Basically a programming library as its name implies is nothing more than a group of code routines that are ready to be used or reused at will, and it is in this last concept where thinking about libraries makes sense, think about having to copy and paste your functions from one file to another every time you want to do a new implementation, it could be very demanding and quickly get out of control as your projects grow. Wouldn’t it make sense to put all these codes that are used project after project in one place?

The miracle of libraries

As we have mentioned, libraries allow us to compile code routines that we can categorize and reuse whenever we want, gaining portability (you can share them) and greater control over their update and support, it is better to modify a single library than thousands of files that use it .

C Static libraries

Speaking specifically of libraries in C we find two types: static libraries and dynamic libraries. In this post we will address the first.

A static library is one that is composed of object codes ready to be joined with our file in the linking process. We find several interesting points in this:

  1. The source codes of the different code routines that the library contains are already compiled into binaries.
  2. These binaries will only be mixed with our files in the linking phase of compilation.
  3. Thanks to dealing with object files the compilation process becomes much more efficient (speed)

What a library looks like?

Generally a static library will be found on the system in the directory /usr/lib/ under the file extension .a in GNU Linux systems. Think of it as zip file, which instead of files of all kinds saves only object files .o as seen in the following image.

Files embedded in a library

To see the content of your library you can use any of the following commands …

ar -t libLibraryName.anm libLibraryName.a

How to create a library?

We all have the ability to create our own libraries, for this the only thing you should have is a group of binary files already compiled .o and follow the following steps:

  1. Create the library using from your object files using the following command …
ar rc libLibraryName.a *.o

Note several things in this line of code. First, the command used to create the library is ar, this is the program in charge of te process, the r flag ensure replace any object file that may already exist within the library and the c flag of creating it if it doesn’t exist. All library files must begin with lib and end in .a, hence the name that becomes the first parameter, the second, as evidenced by the command, would be all the .o files that you want to include in the library. Very easy, don’t you think?

2. The second step would be indexing, think of this process as the index of a book, without it it becomes difficult to see efficiently what information the book contains. To index the library enter the following command in your console.

ranlib libLibraryName.a

Your library is ready!

How to use a library?

It is useless to use a library full of great routines if we do not use them in our files, to make use of them we just have to invoke them in the compilation process and that’s it! Your executable now has all the routines from your library built in, using them at runtime.

To link it to your file, just use these options in your compilation where -L indicates the path of your library and -l the first part of the name of your file (lib) and its extension.

gcc main.o -L. -lLibraryName -o prog

If you have any questions, don’t worry, here is a good video that explains the whole process in a simple step-by-step way.

I hope this article has helped you to become more familiar with the concept of libraries and the meaning and use of the particular concept of static libraries in C, if you liked this topic I encourage you to share this article and expand the knowledge. See you soon…

--

--