Static Libraries in C.
When thinking on how to get the most of some of those portable cleaver solutions you find to problems when coding, creating your own library (or libraries) is something you should seriously consider.
There are basically two types of libraries in C: Statics and dynamics.
Here we are going to cover the basics of Statics libraries from gathering the functions your want to use, to its use in a final program.
Static libraries are “added” in the linking step of the compiling process (for more info about the compilation process, check this link)
We can think of a static library as useful set of tool we developed and want to keep in a toolbox to use when necessary.
A library is a set of C programs we have compiled into object (.o) and put together into a kind of executable file that instead of running directly, its functions are called with parameters from our executable.
The main advantage of static libraries is that all the content you need to run your program is stored in it, since the library added to the final executable file when it is compiled.
The downside comes mainly with the big size of the final program file and the difficult to upgrade it, as you will need to rewrite your functions and go through all the library creation and program compilation process every time you do any changes to your functions.
But, lets get to work and learn how to create a library.
Gathering the tools
The first logic step would be: list and gather those tools, so, it is advisable to get all (and only) the C files your are going to use in you library and the Header file listing them (don't forget to include the macros ifndef<HEADERFILE>_H and define <HEADERFILE>_H at the top and bottom, this way the header files is only defined once and not every time it is called.)
Then run:
$ gcc -c *.c
This tells C to compile our library code into an object file (-c) all (*) of the .c files in that directory at once.
You can instead choose which file(s) you want to compile by running:
$ gcc -c your_file_name.c
Now that we have all our .C files compiled into object files (.o), we will use the ar command to create our library, this is kind putting all those files in a single one. We give it a name (library_name in this example), add the “lib” prefix to it, and end it with the .a extension.
Simply run:
$ ar -rc liblibrary_name.a *.o
Where c is the option used to create an archive and insert all objects files (*.o) replacing the older files where needed (r).
Now that we have our library, and depending on our computer system or archiver, we want the compiler to quickly reference the symbols to speed up the finding references process, this is done by indexing it, this is specially true since a library may contain hundreds or even thousand of symbols.
This is done wit the ranlib command.
Continuing with our example we would run:
$ ranlib liblibrary_name.a
We have successfully created our library and indexed it (if necessary).
To check its content we can run
ar -t liblibrary_name.a
and / or
nm liblibrary_name.a, if we want a lists of each symbol’s name, type and name of the object (.o) files.
Recapping what we have done and learned so far to create a static library:
- Gather the functions you want to add to a library.
- Compile all the .c files into object files ($ gcc -c *.c or gcc -c file_name.c)
- Create the static library (ar -rc liblibrary_name.a *.o)
- Index the library (ranlib liblibrary_name.a)
But how do I use it?
To use your library, you need to tell the machine to look for it and what to look for(library name), this is done when compiling the program that is going to use the library.
Compile the library with gcc
using the -L and l
flags and your library name:
$ gcc main.c -L -llibrary_name -o program_name
Now you executable file named program_name, contains, besides your source code, all the functiones inside the c files added to your library_name.a.
ENJOY!!