C static libraries.

Carolina Andrea Capote
4 min readFeb 27, 2021

There are two kinds of libraries in the C language, static libraries, and dynamic libraries. On this occasion, we are going to talk about the first kind of library.

But I can imagine that you are wondering, what a library is. Well, a library is a collection of functions, structures, variables, etc. organized into a file. The files contained in the library must be object files and you can recognize them because they end in .o extension (file.o).

Why should you use libraries?

The static libraries are very useful because they group a bunch of structures and functions in the same place. Below, you can find some good reasons to use static libraries.

  • First of all, a library makes it easier to reuse structures.
  • A static library makes the code more portable because you don’t have to depend on particular libraries that are on the system.
  • The library would hold every change you don’t want to have to keep recompiling it every time you build your complete program that uses those functions, so you maintain them separately and only change them in the library when you need to.
  • Linking a program whose object files are ordered in libraries is faster than linking a program whose object files are separate on the disk. Once you use any of those files, the system has fewer files to look for and open, which speeds the linking.

How to create a static library?

To create a static library, you only have to follow two simple steps.

  • Take into account that the files in the library have to be object files. Generate the object code of all the functions (file.c) you want to include in the library compiling them with the -c flag.
  • Use the “ar” command to create the library. ar is for archive files, such as static libraries.

Once you finish, you will have created a library called liblibraryName.a

How to use a static library?

When you finish creating the library, you can use it by adding the library’s name using the -l flag in the compilation process.

How static libraries work?

It is actually very simple. During the compilation process, the linker takes all the object files that our C program needs to run and includes them into the executable file, including the functions in the static library.

On this image, you can see the compilation process where the linker includes the libraries in the executable file. In the example, you see a library that contains 5 object files. The libraries are archive files that end with the .a extension.

But, why the linker has to include those files? When we use a function that we previously created, we only have to invoke it. For this purpose the function has to exist, it is a C program (file.c). The files in the linker process have to be object files, that is why we have to create an object file of all our functions including in the static library. We take the c program “file.c” and create the object file of this file “file.o”.

In conclusion, the functions that we program can be linked in the program and then are included in the executable file of the C program, the compiler copies them into the executable file of the C program so that grows your executable depending on how many functions you use and include.

To learn more about compilation visit: https://medium.com/@2621/what-happens-when-you-type-gcc-main-c-91cbb4acf609.

--

--