Let’s start with a simple question: What are libraries?
Well, in simple words, they are the function that you use by #including some libraries like <stdio.h> or <stdlib.h>. By including these libraries you got access to many functions that you can use to help you make your program. Not only functions can be stored in a library, but the majority of these libraries only got functions in them. These libraries got some differences between them and that depends on what do we want and how do we create them.
Then, what is a static library?
A static library is the combination of object files that are linked and compiled together to get an executable. This executable will have all the function you need for your program, saving time in the compilation. Or that was the reason why it was created. On these days there are many fast compilers that it doesn’t matter so much right now. This is why Dynamic Libraries are better in many ways but for the moment, let’s get into more details with Static Libraries.
So, another reason for developers to create the static library is because if they don’t want to share their source they can permit programmers to link to their library but of course this is only an advantage for the vendors and not for the programmer who’s trying to use the library. This is why Dynamic keep winning against Static libraries.
The prefix for the names of your library should be “lib” for the compiler can find the right one using -l flag and the suffix will be “.a” on linux operating system or “.lib” on windows.
Let’s create our first Static Library
Creating our static library is fast and easy. First we need to compile all our function code (.c) into object files using GCC with -c option like this:
Now that all our c files are object files we can just create the static library using this:
This will create an archive using the option -c for that purpose and will add all the object files into the library using the -r option (this will too replace object files with new ones if it needs to).
So there’s something peculiar with the static library and this is “ranlib”. This command, “ranlib”, will make a header in the library with the symbols of all the object files. It will help to compile faster because it will help to quickly reference symbols. This will not be necessary but that will depend on your computer system or your archiver.
To see the content of our library we can use the option -t with the command ar like this:
This will show the object files in our library like this:
To see the symbols of our library we need to use “nm” command to list the symbol value, symbol name and symbol type for the object files. You will see something like this:
So we just created our library and we already to test it out. To check if our library work, we need to compile a program with our library but we are going to use some options in this compilation like “-L” and “-l”.
- The “-l” option will just let the compiler to look for the prefix “lib” so it will shorter the compilation time.
- The “-L” is to specify where we want the compiler to start looking but by using “-L.” we will let the compiler to start looking from our current directory.
Compiling a program with our library will be like this:
I just named the executable with “quote” but you can name it however you want using that option “-o”. As you can see, “-l” will just take care of the prefix and suffix of libholberton.a, so you just need to put the name of the library only. By using “-L.” we are letting him know that our library is in our current directory. The output of this all this will be this:
So, what are the advantages and disadvantages of this kind of library?
- It’s speed since it got all the object files inside the library (they live together) so calling functions at run-time is faster.
- You will need to recompile your program after fixing bugs in some functions.
- The size is larger because of the neccesity of recompile every time you add a new code to the executable.
So, this is all I got for the static library for the moment but this process is pretty much straight-forward and easy to understand. Until then, happy coding!