Static Libraries in C
Why we use them
Libraries are a tool provided to us by compilers. They are a file that has several objects. This simplifies the linking phase of compiling since a library allows many objects to be treated as a single unit. Libraries allow us to access many functions to use in programs without having to include the whole function in our project file. It also allows us to have less files involved in the process. Rather a library points to the location where the already existing function is. This allows us to keep out code clean and to be more efficient on space and time. We can create a function once and not need to recreate that function every time we need it in a new program.
How they work
Static Libraries are, at their simplest level, a list of functions and the appropriate links to those files. They simply direct the compiler to the correct function needed, but have no active role during run time. They provide the compiler with a link to the function that is being called in the program and the compiler then adds that function to the executable code.
How we create them
A group of objects, a file with them all listed and some command lines can create your own library to use with your programs.
The “Archiver” is a command prompt tool for creating libraries. It is used as “ar” when calling it in the command line.
- ar rc (library name with “.a” extension) (compiled files with “.o” extension)
- Generic Ex: user$ ar rc lib.a *.o
The generic example is calling the archive program with both -r and -c flags. The -c creates a new library with the .a file name, if one with the name does not already exist, and the -r makes sure that if there exists files with the same .o name, they are replaced with the newest version.
This has created the library, but it is not the last step. The archive exists but it needs to be indexed in order for the compiler to be able to use it.
- ranlib newlib.a
ranlib is a command that indexes the library we just created in order to speed up the compiling process. Indexes are created so we do not have to use symbol lookup. Ranlib is not always necessary, but that will depend on your system. It can also (and should) be used to re-index when changes are made to the archive (either adding, deleting, or modifying existing objects within the created archive.)
How to use them
We have created a wonderful think, but without some other steps, it is a useless list. Libraries must be included in the compiling process. If you are using gcc a compilation prompt using a library might look as follows.
- gcc fun.o -L. -lnewlib -o funexe
So the “-L.” flag when compiling to alert the compiler that a library is going to be used, and needs to be linked. The -L only says that the compiler needs to look for a library in a directory, the “.” tells it that the library exists in the current directory. Additionally the the -l(libraryname) tells the compiler what name to look for. The above call takes the file fun.o and links the library in the compilations and puts out the exectuable funexe.