Static libraries
A static library is an object file that contains several object files, which can also be used as a single entity in a linking phase of a program. Simply put, static libraries allow users to link to programs without having to recompile their code, saving compilation time.
Here, I will show you how to create your own static library for your programs.
To convert all .c files to object files (.o) at once, you can apply this script at the prompt:
Now, you have all the object files you want to add to your library. So to create a library and add those files you have to use this simple script in the directory where all the .o files are:
Finally, as physical libraries we have to index or order the information we have there. This is good practice because it will make the linking step faster:
We have now created a static library libmy.a and this allows us to use it by invoking it as part of the linking process when creating an executable program. We can do this easily, you just have to use the flag when you apply the gcc build code:
· -l: library name without prefix and lib extension.
· -L: specifies the path to the library. We can use -L. to point to the current directory and -L / home / static to point to the / home / static directory.
Static linking is useful, but it has two main disadvantages:
· If your library code is updated, you must recompile your program.
· Each system program that uses that library contains a copy in its executable. This is very inefficient and gets worse if you encounter a bug and have to recompile.