Static libraries!
Why use libraries in the programming world?
A good way to understand why use libraries is when we are making a program and it becomes too long and some segments of the code become repetitive. Having a long code we have to take in mind the compiling process time and how to be more recursive to optimize, the best way to solve a problem is to divide it into smaller problems.
With a library we can make code segments that may be needed in many programs, like mathematics functions or read inputs of the keyboard, the libraries within contain variables and functions, which can be imported into a program that requires something from them.
How the libraries work?
The libraries are one of the tools of the compiler, these contain for the most part object code and can be used as a single entity in a linking phase of a program (read more here!). All libraries are ordered by index to make it easy to find symbols (Variables, functions and so on) within.
Static libraries are just collections of object files that are linked into the program during the linking phase of compilation, and are not relevant during the runtime.
In conclusion, linking a program whose object files are ordered in libraries is faster than linking a program whose object file are separate on the disk. Also, when using a library, we have fewer files to look for and open, which even further speeds up linking.
How to create a static library?
Is "ar" (archiver) the basic tool to create a static library on Linux. 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. The syntax looks like below:
The command creates a static library named "libexample.a" and put a copy within of the object file "example.o". The flag "r" replace older objects files in the library with the new objects, the "c" flag create the library if it doesn't already exist.
After an archive is created, or modified, there is a need to index. The index is used by the compiler to check the correctly order of the symbols in the library, so to index the archive we use:
Now that the file is indexed, you can check what object code contains, there are two ways, using ar with the flag "-t" or using the command "nm" see below:
with ar command:
with nm command:
NOTE: when an archive file's index generation date (stored inside the archive file) is older than the file's last modification date (stored in the file system), a compiler trying to use this library will complain its index is out of date, and abort. There are two ways to overcome the problem:
- Using "ranlib" to regenerate the index.
- When the file is copied, use "cp -p" to keep all attributes of the file, including its access permissions, owner (if "cp" is invoked by a superuser) and its last modification date.
How to use static libraries!
We will use a function to print a phrase from Kevin Mitnick on the terminal, the main code is:
As we can see, the way to print the quote is using a function _puts. So, inside of example.o is the function to print write in object code. Like a current function we need to have the prototypes of the functions in a .h file, in the example is "holberton.h" that contains the prototype of _puts.
The next step is compile, using this:
The compiler creates a program with the object code of example.o and the any symbols that the program need int the library example, in the compile step we don't need use the prefix "lib" and the extension ".a". The "-L." flag tells the linker that libraries might be found in the given directory.
Now just run the program to check: