C static libraries
When we coding programs in C, we realize that often some parts with the same code are used in many of them. For example, we can have several programs that use complex numbers and the functions of addition, subtraction, etc. are common, or also we realize that we are repeating again and again the code to print a message or calculate a specific value.
Why use libraries?
C is great and able us to put those functions in a separate directory of the specific programs and have them already compiled, so that we can use them whenever we want, which give advantages like:
Not having to rewrite the code (or copy-paste).
We will save the time to compile that code that is already compiled. In addition, we already know that while doing a program, we test and correct, usually we have to compile many times.
The code already compiled will be tested and reliable. Maybe not the first few times, but when we have already used it in several different programs and we have corrected the errors.
The way that we do this is making libraries. A library is one or more functions that we have already compiled and prepared to be used in any program that we do.
How do libraries work?
In a static library, the linker includes the object code of the library functions your source code uses.
The compiler links the code of the used functions from the Static Library and appends it to the executable file, therefore the executable keeps all the needed code to run on its own without other additional files.
How to create static libraries?
Below you can find how to create static libraries using C in GNU Linux.
- Code your functions in separate C files “file.c”
- Compile the C files as object files “file.o” or “file.obj”. We can use GCC with the parameter -c “gcc -c file.c”( the -c option stop the compiler in producing the .o files), the compiler will create the objetc file “file.o” based on the C file “file.c”
- Create the library (for our example we will name the library as libra.a)and add the object files, using this command “ar -rc libra.a file.o”. The ar command create the archive file (libra.a), -r is the parametrer used to replace-overwrite .o files (in case library exists already), -c creates the library if it doesn’t exist or appends to it if it does.
In this moment we have the library libra.a created.
How to use static libraries?
If you want to use the library that we have created “libra.a” is necessary to compile the main C file (for our example we will name this as main.c) with the library, for this we should use the command “gcc main.c -L. -lra -o executable”, the -L. option tells the compiler to look for Libraries and the path to look at, -lra is used to declaring the library’s name that we want to link (note that our library’s name is libra.a but we typed -lra, the linker needs only to know the custom name we give it and it will add automatically the preffix and extension),-o executable specifies the name for the executable name (in this case we have named it as executable but you can write other name), if we do not use the -o option the compiler will create the default output name a.out.