WHY USE LIBRARIES?
If we think for a moment about a library, we could conclude that libraries store information, but this would only be the tip of the iceberth, since they have the ability to organize, list and maintain functions and declarations in one place to their simultaneous use, thus helping programmers and programs to use them more efficiently.
When we program the libraries, they make our work easier and less repetitive, since it involves those functions that we use on a recurring basis. Libraries come in two static and dynamic types, but for now we will focus on static ones.
STATIC LIBRARIES.
These libraries are made up of object files or ".o", or so to speak files that have not passed through the linker, that is, they are files that are not yet executable, object files are an essential part of the linking process when compiling a program, but they are not part of their execution time,
The efficiency of libraries lies in that by organizing the object files necessary for linking a program and keeping them indexed in one place, it avoids that our machine has to do searches in different parts of the memory, translating this into a saving of time by part of the compiler. Now we think that libraries are perfect for their efficiency, but all this work translates into a cost in exchange since the files resulting from the compilation, that is, the executables, are usually quite large, in addition to updating the library, it must later be recompiled. the program for the update to take effect on it.
how to create a library
To create a library, use a command called "ar" which creates a program with extension ".a".
The first step in the process is to have the ".c" files located in the directory where the ".a" file is going to be created. To copy the files from one place to another, it is recommended to do so with the command " cp -p "since this makes our compiler think that the files to organize are up to date, then we go on to create the object files, the command used for this purpose is" gcc "followed by a flag that is used to create these files which is "-c" represented in our work environment like this:
$ gcc -c *.c
here what we did is create our object files telling gcc to take all files ending in ".c", and convert them to ".o" files.
followed by this we use these object files to create our library using the command "ar" accompanied by its flag "-rc", the "c" flag tells "ar" to create the library if it has not been created yet, and the "r" flag tells "ar" to replace the old library files with new ones. To create the library the syntax of this command is like this:
$ ar -rc libfile.a * .o
for this example we will do it with a library that we will call "libholbertonschool.a".
Now that we have created our library, it is time to use it.
To see the contents of our library we use the command "ar -t" as follows;
$ ar -t libholbertonschool.a
In case our library is not indexed we can use the "ranlib" command to do it, this command creates an index of the contents of a file and adds it to it.
in this case it would be as follows:
$ ranlib libholbertonschool.a
To use the library we have created a file main.c that will help us to link a program that prints a predetermined phrase and in an ordered way.
when we try to compile the main.c file we have a message like the one in the following image and it stops the process completely
so how to make it work? it is simple what we do is link our main.c file with our library, and voila. and we do this in the following way:
$ gcc main.c -L. -lholbertonschool -o phrase
In the previous command we link the main.c file with the holbertonschool library, but if we look at the name of the library it does not have the prefix "lib" and neither does the ".a instead it has -l that specifies the library and has the flag "-L" that specifies the address, also after the library we have put "-o" that helps us to establish the name of the executable file where all the process we perform will end and in this chaos is the file "phrase" .
Then we use "./" to execute the phrase file and voila we have an executable program that we link to with a library.
$ ./phrase