The word library problably takes you to a big, charming and sometimes magical place -just like the picture- where tons of books are stored and available for you to read. If you take that image, but instead of a place, you think in a computer -which is always magical for me- you will realize that everything inside it needs to be stored somewhere, let’s say, a library, because, in any case, a library is a collection of sources of information.
Getting more specific, in C language, during the process of compilation, as I explain in a previous post, in order to make a C file executable, the compilator goes throuhg some stages and at some point it has to look into C libraries to find the instructions described in a program. In C, a library is a file or a collection of object files (a file created by others or by you containing machine code from either the assembler or compiler process) that contains variables and functions to use in a program, this enables you to perform different things such as taking input from the keyboard, handling with numbers or creating math operations, therefore libraries are clasified according to the the type of action their functions do, just as books libraries are clasified by topics. So we can say that C has more than 25 “topics”.
Maybe you think: ok, but why using libraries? Well, imagine if every time you go to a library looking for some books you have to check each shelve because there is not a clasification by topic, that would be crazy, huh? Or if when writing a program with specific instructions you need to create a fuction for everything, call every library, that would be kind of boring and a waste of time; instead, you can just call the library with the “topic” you need and voilá: you have more time for coding.
A C library indexes a group of functions in object code (machine language) inside an archive. This index has to be updated, and has to allow the program using the library to easily indentify and find those object files. C libraries use a header file, whose extension is “.h”, as an interface expressed file to be called from within the user’s program. It informs the program how to call the functions and their basic characteristics through function prototypes. At the begining of any program, the header file must be called in for built-in libraries or your own libraries.
In C there are two types: static and dinamic.
A static library is a collection of files linked to a program during the linking phase (remember that the compilation process has 4 phases: preprocessing, compiling, assembling and linking). Here, the process searchs in the libraries where the functions inside the code are located, so object code of the program is combined with object code of the selected libraries. For example, if you use a function to print, such as printf, the linker will look for it in the stdio.h library but if you don’t specify it, the program just won’t run. Another thing it does is to verify that only one library contains the main function.The output of the linker is an executable file so when you run it the your result will appear.
- In order to create a static library, first you create a header file with .h extension where function prototypes are kept; then it is necesary to use a program called “archiver” or “ar”, that allows you to do more things like modify and list the libraries.
2. Another thing needed is to convert .c files into object files, this is is easy to do using -c flag like this:
gcc -c file.c file1.coutput: file.o fileq.o
3. The syntax to create a library is as follows:
ar -rc lib_name_of_library.a file.o file1.o
4. At this point you can index you library, this means 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 compilation. The command to create or update the index is called
'ranlib', and is invoked as follows:
5. Next, it is importat to tell the linker where the library is located, for this you can use a -L flag; then it is also important to tell what file will be included using the -l flag (-l flag will add lib and .a to the name of the library). Here is an example:
gcc -L /path/to/library -lname_of_library
A dynamic library, on the contrary, is conformed by separate object files, each of one has pieces of code which later, when called, will be linked together in a dynamic way. These libraries are more efficient and easy to create, use and update. When using a dynamic library, during the linking stage, functions are not put into the program’s code; they are only referenced through an address, this means that when the process is happening, the operating system will search into that address and then the functions will be executed. This is helpful because a big program could call libraries many times and it is more efficient to use addresses than static files. These libraries are easy to update because, like almost everything in IT, libraries can be improved anytime specially if they are dynamic and there is no necesity of comipling your program again if this happens, this is done automatically. This is also important due to your program is using less memory.
To create a dynamic library, you have to follow the first two steps described for the static one, but in the second it is necesary to add a flag at the end of the line: -fPIC, wich means Position Independent Code, what it tells is that the generated machine code is not dependent on being located at a specific address in order to work (more here):
gcc -c file.c file1.c -fPIC
The third step is to create the library with .so extension using the following:
gcc file.o file1.o -shared -o libcreation.so
The shared flag lets the system know that it is a dynamic type; the -o flag lets you name your library.
Then, you can export your library, this way programs will know where to go when they are executing, doing this:
To compile your file with the library, go to step 5 in static libraries:
gcc -L file.c -llibrary
Finally, I show you some commands to work with dynamic libraries: ldd is used to list all libraries a program is gonna work with; ldconfig generates the necessary links and cache to the most recent shared libraries found in the directories specified on the command line (more here) and nm is to check libraries or shared-object files and display their contents, or meta information stored in them, specifically the symbol table.
So, if you are not tired enough, I recommend you to read more about libraries here: Building And Using Static And Shared “C” Libraries or here.