What is a C static library?
A library is a file containing several object files, that can be used as a single entity in a linking phase of a program.
Why use libraries?
Because the library is indexed, so it is easy to find symbols (functions, variables, and so on) in them. For this reason, linking a program whose object files are ordered in libraries is faster than linking a program whose object files are separate on the disk. Also, when using a library, we have fewer files to look for and open, which even further speeds uplinking.
How they work?
A static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable. It is helpull to avoid compilation of function every time you want to test a main.c . After it , you only need to test the library file with the main:
How to create them?
You can create coding these steps:
The basic tool used to create static libraries is a program called
'ar', for 'archiver'. This program can be used to create static libraries (which are actually archive files), modify object files in the static library, list the names of object files in the library, and so on. In order to create a static library, we can use a command like this:
gcc -Wall -pedantic -Werror -Wextra -c *.car -rc libholbertonschool.a *.oar -t libholbertonschool.a
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 won’t matter during compilatio. The command used to create or update the index is called
'ranlib', and is invoked as follows:
How to use them?
we should bare in mind that the order in which we present the object files and the libraries to the linker, is the order in which the linker links them into the resulting binary file. First “-L” then “-lholbertonschool”
gcc main.c -L. -lholbertonschool -o alpha