About C libraries
What are libraries?
In programming libraries are collections of functions that we used in a basic program like a library of books, it’s like precompiled routine that program use. In C library is a collective of header files stored for the use of other program and library files have a special extension “ .h ” and we call them standard library exemple “ <stdio.h>”
Why use library?
Libraries are useful for storing frequently used functions because you don’t have to link them to every program that uses them so we can make use of these library of functions to get the pre-defined output instead of writing our own code to get those outputs.
How they work?
We can compile and ran programs with libraries the -c causes the compiler to produce an object file for the library the object file contains the library’s machine code. It cannot be executed until it is linked to a program file that contains a main function, makefiles make working with libraries a bit easier.
How we create them?
To create a library you should first create an interface for it “mylib.h” second you have to create an implementation to your library “mylib.c” then create an library object file “.o” with the command
gcc -Wall -pedantic -Werror -Wextra -c *.c
this object file can be archived one created for static libraries using ar -rc libmylib.a *.o
or shared one created for dynamique libraries with extension “.so” .Finally we can ran our new library using the command ranlib libmylib.a
How to use them?
Like i said the point of creating libraries is to use them in another C program , so after we created our archive we want to use it in a program This is done by adding the library name in the header of the C file like below
#include “mylib.h”
that links in the libary code into a.out file.
Exemple of Holberton.h library and of the use of it in isupper function: