What in the world are static libraries?
In this long journey of becoming a software developer, the analogies have been one of my multiple allies to understand some complex concepts. Allow me to show you an exaggerated example. Imagine you set out to cook a great recipe. You will need a basic tool-kit right? so, when you need them you just go to your drawer and use them anytime. I’m assuming you have them organized and categorized by type of tools(spoons, knives, etc.)
Well, those drawers, containing tools that will help you to cook successfully your plate, would be the libraries. But wait, here’s the exaggerated part of the example. Imagine that you are willing to create your own personalized tools, by creating them you will make a faster and tastier food, also you can use them not only for today’s recipe but for any other time. Well, those drawers containing the new personalized tools would be the static libraries.
Now let’s try to switch the example to the C language: our tools will be the functions we will create for our programs. And the libraries are the drawers containing compiled functions(that means ready to be used!). There exists two types of libraries: shared and static. But today we will focus just on static libraries.
Why it is important?
If we were to build a big program or application is a good idea to create libraries for a group of functions that could be used in several systems. So, in those systems, we could just include them and use them, without the need it to be part of our code.
But, we can not just update the functions and that’s it? yes, but if we use libraries we would just update them once and rewrite our library.
Where do they fit in?
If we remember what happens in the complete compiling process we would see that each stage will need its corresponding requirements. Let’s see this graphic to see what kind of file generates each stage of the process and thus the file that the next process will need to get done.
In each stage, we can see the type of file resulting
Show me the code!
Alright, now that we have ready our functions, for practical purposes let’s move them all to one single directory. Now we will compile all these .c files with the following command.
$ gcc -c *.c
- $ Command prompt.
- gcc is our compiler command.
- -c is the flag that will generate us the object files from the .c functions
- *.c will use all the files finishing in .c.
As a result, we will have the same number of .o files from the .c files. Now, this .o files will be the raw material to create the static library.
Let’s type now:
$ ar -rc libname.a *.o
- ar is a Unix utility that maintains groups of files as a single archive file(we’ll “save” our .o files here).
- -r flag to insert the objects and replacing older files if needed.
- -c flag to create an archive.
- *.o it will take all the object files in the current directory.
To finish this piece of art we need to index the library, to do so we will use the following command:
$ ranlib libname.a
Well, we have ready our tools, now let’s use them with an actual main function, to do so you will need to type
$ gcc main.c -L. -lname -o main
From now on you can use easily the functions included in this library and if you want to update them you will just update and overwrite the files in the libname.a
I hope this gave you a better overview of when talking about static libraries!