In past article we explained the compilation process of a C source code to an executable file. We went through the compilation stages and how the C code is preprocessed , compiled into assembly code(.s) , assembled in to machine code(.o) , then goes through the linker where some extra code is added (.lib .a) and we get our executable file. Now in this article we are going to explain what is that extra code , why and how we use it. Basically we are going to explain and understand Static Libraries.
What are Libraries and what is a Static Library
In computer science a Library is a set of routines, external functions and variables that we store in an indexed archive (file). You may have noticed that we are using functions which are not defined in our code, or in that particular file and we include a header file in the top, that contains declarations of those functions. For example everyone that have wrote a code that prints a message on screen , used printf(); function and included stdio.h to make it work. There are Dynamic and Static libraries.
Well a Static Library is this archived collection of functions which are resolved in a caller at compile-time and copied into a target application by the linker producing a stand alone executable. This is process is also known as a static build of program.
So basically the linker (compiler) takes the code only of the used functions of the Application Code from the Static Library and appends (copies) it on the produced Executable file. This way the executable can run on its own without the need of any other file to come along.
Now lets understand better how it works, a static library is purely a collection of .o files, put together in an archive. When you use it for linking, the linker will search the library for .o files that provide any of the missing symbols in the main program, and pull in those .o files for linking, as if they had been included on the command line like .o files in your main program. This process is applied recursively, so if any of the .o files pulled in from the library have unresolved symbols, the library is searched again for other .o files that provide the definitions.
How to create and use a Static Library
In order to create the library first thing we need to have all our functions in separate C files (function.c). After we are done writing-composing-coding our C files we will have to compile them to object files (.o or .obj). We do that with the following command in shell command line:
gcc -c functionname.c
“gcc” is to call the compiler
“-c” is to stop the compiler in producing the .o files
in the end we give the .c file we want to compile
In our case we want to compile all the .c files we created and compile them with all error checks and so it would look like this :
gcc -Wall -Werror -Wextra -pedantic -c *.c
At this stage we have created our .o files and we are ready to go create our library and add them. To do so we will type :
ar -rc liball.a *.o
“ar” command to create archive file (.a)
“-r”a flag to replace-overwrite .o files (in case library exists already)
“-c”a flag to create the library if it doesn’t exist or appends to it if it does
“liball.a” how we want to name the library but always with the lib- prefix and .a extension.
“*.o” to add all object files (we can choose one or multiple by typing their names)
At this point we have successfully created an archive file that contains the object files we want. That is a Library!!
To use this library now we going to compile our main.c file (the code for the application we want to create) and declare the library to our compiler.
To do so we will use the following command :
gcc main.c -L. -lall -o program_name
“gcc” to call the compiler
“-L.” flag to tell the compiler to look for Libraries and the path to look at
“-lall” with this flag we are declaring the library’s name that we want to link (note that our library’s name is liball.a but we typed -lall, the linker needs only to know the custom name we give it and it will add automatically the preffix and extension)
“-o program_name” flag -o defines the executable’s output name (note that we could run the command without the use of this flag and get the default output name “a.out”)
I hope this article helped you understand how to create and use a Static Library in C and specially what is it.