All you need to know about C Static libraries
What is a library?
Library in C is a collection of header files, exposed for use by other programs. The library therefore consists of an interface expressed in a .h
file (named the "header") and an implementation expressed in a .c
file. This .c
file might be precompiled or otherwise inaccessible, or it might be available to the programmer.
What is a static library and how does it work?
A static library is a file containing a collection of object files (*.o) that are linked into the program during the linking phase of compilation and are not relevant during runtime.
As shown in the diagram above, when a program is compiled, the compiler generates an object file from a source file. After generating the object file, the compiler also invokes the Linker. The role of the linker, in this case, is to copy the code of the library to our object file.Basically, static libraries are just a collection of object files that are merged by the linker with another object file to form a final executable.Conventionally, they start with “lib” and end with “.a” or “.lib” (depending on your platform).
How to create static libraries?
To create a static library, we need to specify to the compiler, which is GCC in our case, that we want to compile all library codes (*.c) into object files (*.o) without linking. To do that we are going to use the command below.
$ gcc -c -Wall -Werror -Wextra *.c
Flags description:
-c: Compile and assemble, but do not link.
-Wall, -Werro and -Wextra: These aren’t necessary but they are recommended to generate better code.Note that the “*.c” matches all files in the current working directory with the “.c” extension.
let take two c files, add.c, and mul.c which make respectively the addition and the multiplication of two integers, and a header file that contains the prototypes of these functions. The picture below shows the output generated after using the command.
Once we have object file(s), we can now bundle all object files into one static library.
To create a static library or to add additional object files to an existing static library, we have to use the GNU ar (archiver) program. We can use a command like this:
$ ar -rc libname.a *.o
This command creates a static library named “libname.a” and puts copies of the object files “add.o” and “mul.o” in it. The ‘c’ flag tells ar to create the library if it doesn’t already exist. The ‘r’ flag tells it to insert object files or replace existing object files in the library, with the new object files.
After an archive is created or modified, there is a need to index it. This index is later used by the compiler to speed up symbol-lookup inside the library and to make sure that the order of the symbols in the library will not matter during compilation. There are two ways to create or update the index. The first one is, by using the command ranlib.
$ libname.a
or by adding an extra flag (-s) to the ar command and it becomes like this:
$ ar -rcs libname.a *.o
In order to list the names of the object files in our library, we can use the ar command with -t flag:
Why use libraries in programming c:
C Standard library functions or simply C Library functions are inbuilt functions in C programming. The prototype and data definitions of these functions are present in their respective header files. To use these functions we need to include the header file in our program. For example, If you want to use the printf()
function, the header file <stdio.h>
should be included.
#include <stdio.h>
int main()
{
printf(“Catch me if you can.”);
}
If you try to use printf()
without including the stdio.h
header file, you will get an error.