C static libraries

Rafael Dorado
4 min readOct 14, 2020
Making a library

A library is one or more functions that we have already compiled and prepared to be used in any program that we make. You have to have enough eye when we do them so as not to put any dependency on something specific in our program.

Static and dynamic libraries
In Linux we can make two types of libraries: static and dynamic.

Static library: A static library is one that is copied into our program when it is compiled. When it is compiled and we have the executable of our program, the library is not necessary for the program to work correctly, we can even delete the library and our program will continue working, since it copied what it needed from the library. For example, if the library has two functions and our program only calls one, only that function is copied.

Dynamic library: A dynamic library is NOT copied into our program when compiling it. When we have our executable and we are executing it, every time the code needs something from the library, it will look for it. If we delete the library, our program will give an error that it cannot be found.

Why use libraries?
One of the advantages of using libraries is that we avoid writing a function several times or copy and paste. When using a compiler program with static libraries the program is faster in execution, when you need a function from the library you already have it in your code and you do not have to re-read and look for the function in the library to be able to execute it, in addition to that we can take the program to another computer and it will continue to work without problems. when we use a library, we have fewer files to search.

How they work?
It’s like a program, only it doesn’t have a main () function. It contains typical functions to be called from some other program or library. Any developer can create a new library.

How to create them?

In order to put our code in a library, we need to organize it in the following way:

  • One or more source files .c with the code of our functions.
  • One or more .h header files with the types (typedefs, structs and enums) and prototypes of the functions that we want to be able to use.

To show how to create them I will use an example in basic.

First we create our header file:
Our header file will contain the prototypes of the functions.

Header file

The code for the library, called print_name.c that will contain a function to print my name:
Este archivo contendrá el encabezado holberton.h que es donde tenemos el prototipo de la función print_name().

Library File

To create a static library we must first compile the print_name.c file to generate an object file, for this we will use the following command:

Executing the command generates an object file print_name.o
Then the archiver (ar) is used to produce a static library (called libprintname.a) from the file object print_name.o.
Note: The library must start with lib and have the suffix .a

How to use them?
To use the library I will create a main.c file that will contain a main function to call the print_name(); function from the libprintname.a library that we just created.
This main.c file will contain the holberton.h header, which is where we have the prototype of the print_name(); function

Now I will create the executable file using the following command and I will check if the code output is correct.
Note: The first three letters (lib) are not put, as well as the suffix (.a)

At this point we can eliminate (if you wish) the library libprintname.a since as I explained above, if the static library is eliminated at this point it does not affect the executable file, since the necessary functions have been copied to the executable file.

--

--