C Static Libraries
Juan Felipe Buitrago Vargas
Fullstack Software Engineer Focused on Front-end Technologies
What is a library?
A library is a file containing several object files, that can be used as a single entity in a linking phase of a program.
Why use C Libraries?
We use C libraries because they allow faster linking done by the compiler because all the object files are together in one file, as opposed to linking a program whose object files are separate on the disk. And since you don't have to open so many files but rather a single one, it further increases the linking speed.
How they work
Static libraries are collections of object files that are linked into the program during the linking phase of compilation, and are not relevant during runtime. Due to the fact that they are only active during the linking phase, only the executable file of the program is required to run the program.
How to create them
In order to create the library you need to have all your object files ready and preferrably in the same directory. If you already have the object files in your directory then you can just run the "ar" command. However I'm going to describe the step by step process of creatig the library starting with just the files in .c format. If you want to create a local (static C library) you have to follow these steps:
- Move the program files into the directory you are working on (this makes translation and creation of library a lot easier)
- Run the compiler with the option to stop at object files (-C) including all your .c files.
- Once your files are translated to object files (.o extension) you have to run the archiver command "ar" with the options -rc (The 'c' flag tells ar to create the library if it doesn't already exist. The 'r' flag tells it to replace older object files in the library, with the new object files.) this is just to make sure your library is created or updated accordingly. The syntax of the command would be as follows:
ar -rc libyourlibraryname.a *.o
- After you've run the archiver command your library will be created. However, if you want your linking to be even faster, indexing is recommended. to index your file all you have to do is run the "ranlib" command followed by your library name. Syntax shown below:
ranlib libyourlibraryname.a
- Note: if at anytime you update your library by running step 3 again, re indexing would be advisable as if prevents your compiler having issues by saying that the index is not up to date
After following the above steps then your library is ready to be used!
How to use them
In order to use the library we just created in a program we have to add the name of the library to the list of object file names given to the linker, using a special flag, normally '-l'. Here is an example:
- gcc main.c -L. -lyourlibraryname -o program
This will create a program using object file "main.c", and any symbols it requires from the "yourlibraryname" static library. Note that we omitted the "lib" prefix and the ".a" suffix when mentioning the library on the link command. The linker attaches these parts back to the name of the library to create a name of a file to look for. Note also the usage of the '-L' flag - this flag tells the linker that libraries might be found in the given directory ('.', refering to the current directory), in addition to the standard locations where the compiler looks for system libraries.
REFERENCE
Guy Keren. (1998-2002). Building And Using Static And Shared "C" Libraries: AS IS . Recuperado de https://docencia.ac.upc.edu/FIB/USO/Bibliografia/unix-c-libraries.html#what_is_a_library.