What are code libraries.
Re-invent the wheel? No no no…
It is a waste of time and energy, the same happens with computer code.
What are code libraries?
In computer science, a library is a collection of resources used by computer programs, often for software development. These may include configuration data, documentation, help data, message templates, pre-written code and subroutines, classes, values or type specifications.
What are they useful for?
As said before, They are used to reuse code already written, written by someone else (usually by the programmers of the programming language in which you are working), by yourself, etc. So if you have a very large project remember that someone else has probably already written part of the code you need, and you can reuse it without any problem, that’s what libraries are for!.
Different types of code libraries.
There are two types of libraries, static and dynamic (shared libraries), The static libraries are linked into the program during the linking phase of compilation (visit:Compilation process), it means that they are mixed with the compilation process only once. A shared library 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.
How to create code a static libraries.
First of all, you need to define what are the programs that you are going to use in the future, or maybe in your team, there are a couple of programs that you are going to use to start a new project.
1 . Files and programs. It’s important to collect all the C file functions that we are going to save in our library. These C files already contain our program to execute a specific activity, in this case, we are going to save a program that prints the alphabet in lower case (a, z), and another one that prints de digits. (0, 9).
example > ls -1print_alphabet.c
example > cat header.h#ifndef HEADER_FILE
#define header.hvoid _print_alphabet(void);
We also have previously created a header.h file that is going to collect all the functions prototypes.
2 . Create object files. Now we need all our functions to be written in object code (also called machine language). so for this step, we compile all our C programs until the Assembler stage in the compilation process
example > #gcc comand to compile our .c files and create the object files
example > gcc -c *.c
example > #ls command to show the .o files created
example > ls *.oprint_alphabet.o print_digits.o
3 . Create the library. ar is the command that allows us to create a library. in this example, we are going to create a library called libstatic.a and we are going to include all the object files in our working directory. *.o
4 . Verify. We can verify our object files to be included in our library as follows:
example > nm libstatic.aprint_alphabet.o:
0000000000000000 T _print_alphabet
print_digits.o:0000000000000000 T _print_digits
5 . Testing. Lest create a new main.c file, which is going to be the entry point of our new program, task, or project. The main function will be compiled with all of the object code included in our library (libstatic.a). This step does not consider the prefix lib either the .a extension. (lstatic). then we can execute our main program with ./ .
example > cat main.c
example > gcc main.c -L -lstatic -o main
example > ./main
in this way we conclude the creation of a static library.