WHAT IS A LIBRARY
A library in programming is a collection of pre-compiled resources, used by computers programs, such as functions, classes, configuration data, documentation and so on.
Why and When to used Libraries: Libraries provide the user the benefit to used a variate of files that can be reused in different programs. For example If we were going to use the same functions in different programs, we will just write it once and reuse it instead of having hundreds of copies of the same function, one in each program.
A library is basically just a file but it is not executable and that is a key difference from processes and applications. Libraries play their role at run time or compile time. In C programming language, there are two types of libraries: dynamic libraries and static libraries.
Libraries have object files created by the
“-c” gcc flagand end in
“.o”by convention. They are the result of the output of the compiler and contain function definitions in binary form.
What is a Static Library
A static library is a collection of objects files that are linked into the program during LINKING phase of compilation. (Not important during run time); So only the program’s executable file is need it in order to run the program.
What is a share or dynamic library
A shared library or shared object is a file that is intended to be shared by executable files and further shared object files. Modules used by a program are loaded from individual shared objects into memory at load time or runtime, rather than being copied by a linker when it creates a single monolithic executable file for the program.
So How compilation Works:
A dynamic library is linked into the program in two stages
- During compile time: the linker verifies that all symbols required by the program are linked into it or in one of its share libraries.
- During run time: When the program is started the Dynamic loader checks out the libraries that were shared with the program, loads them to memory and engage them in a copy in memory
How to create the static Library
First, convert all source code into object files:
$ gcc -c <source code example: *.c>
Static library filenames usually have a “.a” extension on Unix-like systems.
So let’s use a basic tool to create a static library which is a program call→ ar ← for “archiver”.
ar -rc libName.a objectFile1.o objectFile2.o objectFiles3.o
Don’t forget the name of our library by convention should always start with “lib” and end with a “.a” extension. The -r option will add newer object files into the library if the library already exists. The -c option will create the library if the library does not exist.
Example of creating a static library called libholbertonschool.a from all (*.c )files
A useful command to Up-date the index the compiler
$ ranlib libholbertonschool.a
How to use a static Library in a program
$ gcc main.c -L. -lholbertonschool -o alpha
This will create a program using object file “main.c”, and any symbols it requires from the “holbertonschool” static library. Note that we omitted the “lib” prefix and the “.a” suffix when mentioning the library on the link command. The linker attaches these parts back to the name of the library to create a name of a file to look for.
Where( -L ) specifies the PATH to the library, ( . ) the current directory, and the ( -l ) list of files in the library, and the -o will rename the executable file to alpha.