C Libraries. Make your header files.
It would be amiss to talk about libraries (static or dynamic) without first talking about splitting your program into multiple files and compiling them together. Many engineers and programmers have created programs that use multiple files and compiled them to make an executable.
Splitting a program across multiple files.
While programs such as calculators and even grep seem simple, they are composed of many functions that are called to perform different functions. These functions are declared and defined in one file and the files are compiled together. The prototypes of said functions could be declared in a header file as:
The functions for addition and subtraction could then be declared in a different file:
The preprocessor directive '#include "calc.h''' tells the compiler to include the contents of that file in this file. This way, the functions prototypes can be called from a different file as long as they are included in the header and called together. An example that calls them is using the addition function in multiplication as below.
The code would compile and execute well when called in main and compiled together.
The functions are defined, declared across multiple files, and compiled in this case. Including the header files containing the function declaration before using the functions is essential, or else there'd be an error.
While all these may seem intuitive, it establishes that the compiler can be used to compile multiple files and link files into our source code before coming up with an executable. Static libraries employ this principle during compilation.
Static Libraries.
As we've seen, a library is simply a file, or collection of files linked together with your program during compilation.
Subscribe to my newsletter
Read articles from Samuel Ingosi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Samuel Ingosi
Samuel Ingosi
I am an aspiring software engineer coming to change the world.