C Static Libraries
¿Why use libraries?
Libraries in C are not unlike public libraries in cities, towns, or neighborhoods. A public library provides access to a multitude of information in various media forms to the public for access and use. Functions in a C library can be used and accessed by programmers to create several different programs.
As a programmer, you may find yourself using the same function or functions repeatedly. In this case, it is best to put this function or functions in a library to speed up the compilation of the program. C libraries store files in object code; during the linking phase of the compilation process ( Compilation Process) files in object code are accessed and used. It is faster to link a function from a C library than to link object files from a separate memory sticks or discs.
¿How they 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.
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 them?
To create a static library you have to use the ‘ar’ or archiver program. The idea is that these functions are being archived until needed. A function saved in .c format is recompiled into an object file, .o .
Creating a Static Library step by step:
3. Create a static library. Using “libmy” as an example of a library name, this command creates a Static library:
4. The ‘ar’ is the program being used to archive the files. The ‘c’ tells the program to create a library. The ‘r’ tells the program the replace or update older files in the library. With step three a static library has been created. If needed use the ‘ranlib <libraryname.a> to index the library.
Recommended by LinkedIn
Indexing a library will let the compiler know just how old a library file is. Indexing also gives the library file a header and makes it easier for the compiler to reference the symbols. This step may or may not be necessary depending on your computer system.
To view the contents of the static library access it using: ‘ar -t libholberton.a’ in the command line.
¿How to use them?
We have created a static library and can now use it.
we have a program:
In the header: “main.h” contains function and function definitions used to tell the compiler how to call functionality. It contains “data types and constants used with the libraries”(geekforgeeks). When we compile the file we call the library:
Now we can run the executable:
If the library is working properly the output should be:
We have created a functioning static library.