A library in C is a set of already-made and optimized reusable blocks of code (object files) available in the C programming language’s Standard Library (supplied by compilers) and can be made by users too.
Libraries come and can be made in two kinds…
- Dynamic (or shared) and,
- Static libraries
This post is intended to do justice to everything you’ll need to know about the static libraries available in the C-programming language.
A static or statically-linked library is an external compiled object file that the compiler/linker copies into the target application at compile-time and are not required during runtime since everything will now be contained in a single executable file. It is the opposite of dynamic libraries where codes do not need importing but rather linked directly into the program.
This is the last step in the compilation process…
- Preprocessing stage- at this stage, the compiler processes the #include files, expands MACROS and remove /* comments */.
- Compilation stage- where the preprocessed source code is transformed into assembly language.
- Assembly is the third step that really turns the assembly code into the lowest level known as machine/object code made of pure binary (zeros and ones).
- Linking, for static libraries, linker makes a copy of all used library functions to be available locally and then integrates it into the final executable file.
All this happens in a matter of seconds, provided there are no bugs encountered.
How To Create Your Own Static Library
The command ‘ar‘ (which stands for archiver) can be used to create static libraries as well, list, modify object files (the .o files) in the static library, etc.
ar rc name_of_archive(library) file_1.o file_2.o ...
Create a new directory/folder in your project folder and put all the relevant files into the directory/folder. You’ll want to make sure that your header (.h ) file begins with ‘#ifndef _H’ and ‘#define _H’ in two separate lines at the top and ends with ‘#endif’ at the bottom of the function prototype definitions. This is so that the header file is only defined once instead of each time it is called.
Then in step two, compile all the project source files (that is, files ending with .c) files. Compile them using the ‘c’ (E.g gcc -c file_name.c) option so that the compiler does only creating the object (files ending with .o) file for each corresponding source (.c) file.
Now using the option ‘-r’ together with the ‘ar c’ command, make an archive of all of the object (.o) files, into one static library (.a) file. In case a file with the same name and type already exists, adding the command option -c performs a replace. The ‘c’ flag check if such file already exists before, while the ‘r’ flag does replacement if it actually exists.
All done! Now, instead of having to include all the file names individually during compilation, only adding your static library (.a) file suffices. That even speeds up the linking process.
And there you have it. Lets continue this in the comments block below the post. Thanks and happy coding.