First of all, a library in C is a collection of pre-compiled functions that have been written and need to be reusable. That allows us to use a certain function into a program just by using at the start of our program a header file that provides declarations for library function calls. As a result, you can write your function once and call it as many times you want.
It’s important to remember that the libraries consist only of object files and the filename of the library always starts with lib, followed by their name y finally ending in two ways:
- .a for Static Libraries.
- .so for Shared Libraries.
This time we are going to learn more about static libraries, how to create them, how to use them, and more.
What is a Static Library?
A static library is the collection of object files in a ready to use form. So, when the compiler and the linker combine the program code with the library it results in a single executable program.
But, what does it mean this executable program? Well, that does mean our library has been copied into the program, so if you delete your static library the executable file will still work, but if you make changes into the library that won’t be reflected in the copy inside the executable file.
Why use them?
There are certain advantages when you use static libraries, some of them are:
- You can take your program to another computer and it will execute normally. It’s Portable!
- Your executable program is faster when it uses a static library, due to having the copy of the library in itself and not in another location.
- As said before, if you do changes into the static library this won’t affect the executable. This makes the Static libraries a good option when your programs aren’t so big and kind of simple.
Creating a Static library
Let’s create our own static library. First, we need the functions we want in our library, for example, I have the next files in my current working directory:
I need to compile each of them into object files, so I will invoke the C compiler with the option -c, to prevent the compiler to create a complete program, and use it with all the files that end with .c as a result the command will be the next :
gcc -c *.c
Now I have my files and the object files of them as you can see below
When it comes to creating a static library file, we will need to use the ar command, this put the object files together to form a single file, our library that will be named libholberton.a, with the option r and c (c, to create the archive and r to add or replace older files when needed) on all the files ending with .o as shown:
$ ar rc libholberton.a *.o
Now we can see the new archive that has been created:
After an archive is created, or modified, there is a need to index it. This index is later used by the compiler to speed up symbol-lookup inside the library and the command used to create or update the index is called
'ranlib'. However, on some systems, the archiver (which is not always
ar) already takes care of the index, so ranlib is not needed.
In case you want to see the content of your static library you can use the next command:
$ ar -t libholberton.a
The option -t of ar command, displays a table listing the contents of the archive, like this:
How to use static libraries?
We already have our static library, now we can create our executable file. We will do this by compiling the library with a main file that uses one of our functions, in this case, the object file puts (3-puts.o).
The main file will be:
Now we can create the executable file using the command:
$ gcc main.c -L. -lholberton -o quote
Where gcc is the GNU compiler, main.c is the main file that was shown above, -L specifies the path to the library in this case we just put the . (dot) because our library is in the current working directory, -lholberton the library name that is holberton, and -o to indicate the name of the output that is called quote and is the name of the executable file.
As a result, we execute the file quote and see the result, it should show us that the function puts have been correctly called and the printing of the quote is successful. Let’s see:
Hooray! It works.
That has been how the static libraries are used, hope you have enjoyed and learned a lot from this post. See you soon!