All about libraries in C
When you are using the c language, you always use the standard libraries to perform different functions such as printf, atoi, etc. But how they work really are.
The libraries in the C language are a list of functions, with a table of contents, these are made up of header files.
What is a header file?
A header file is a file with extension “.h” which contains function prototypes and macro definitions to be shared among several source files.
What is the purpose of the libraries in C?
The purpose of the libraries is to add functionality by means of for example in a book that contains some kind of things, you can improve program speed, reading, organization and efficiency.
In C exist two different types of Libraries, the Static and Dynamic Libraries.
A library is static when in the compilation process, specifically in the linker is added to the program.
A library is dynamic when it is referenced in the linker process but not added, and is only used at runtime.
Differences between Static and Dynamic libraries
- It is not necessary to include them when distributing the executable (linker process), because they are not necessary at runtime because they are already part of the program.
- During compilation, linking to a static library is in most cases faster than linking through dynamic libraries.
- It is not hosted in any program, this improves memory utilization.
- Since it is independent, its updates are independent and do not depend on recompiling the program and indexing the library as it should be done with static libraries.
- If you want a faster and self-sufficient program regardless of the memory expense, static libraries are your best choice.
- If you want a better use of memory and the possibility of using the library only at runtime, being independent of the program, the dynamic libraries are for you.
But how can create them
Compile your code for the library, in this case we decide to use all of the files with .c extension in the current directory, and convert into machine code with the -c flag. The use of the other flags are for treat warnings as errors for a better code and compilation.
$ gcc -Wall -Werror -Wextra -pedantic -c *.c
Create a static library from all the files with the extension .o using the command ar the -rc flags are for -r to replace the older files where needed and -c to create the file and insert the objects.
$ ar -rc Static_library.a *.o
Finally we must index the files with the following command. It should be clarified that not in all cases should be done, depending on your computer system or that of your file cabinet. In this case we must do it so that later it is easier for the machine to find all the content of the library, this must be done every time we update something within the library.
$ ranlib Static_library.a
Now to use the static library, we should add the library as part of the compilation (specifically the linking process) when creating a executable program.
gcc main.c -L. -Static_library.a -o main
- -l<libraryname without lib prefix and extension>
- -L : specifies the path to the library .We can use -L. inorder to point to the current directory
Dynamic libraries(Shared libraries)
You have to compile by this way the files you want to link to the dynamic library.
$ gcc -Wall -Werror -Wextra -pedantic -fPIC -shared *.c/executable -o <library_name>.so
GCC is an integrated compiler of the GNU project for the C language.
-Wall -Werror -Wextra -pedantic
This flags are for treat warnings as errors and perfom a better code.
The flags -fPIC -shared , the -fPIC is a condition for a dynamic library, because this put the code in a independient position. The flag -shared creates a shared object(the library) which can be linked with other objects in compilation.
When we refer to object, it can be any type of files such as libraries or header files.
And finally *.c/executable -o <library_name>.so, starting with *.c, this includes all files that have extension .c in the current directory or you can put the executable of your program. The -o flag is to name the library as you wish, the extension .so are for specifically dynamic library.
How can view the content of them?
A form to view this shared library is using the commands *nm* and *ldd*.
The command ldd prints the shared objects (Shared libraries) required by each program or shared object
$ ldd <library_name>.so
The nm lists the symbols from object files, if is not an object the files are listed as arguments.
$ nm <library_name>.so
Now that we have our bookstore we must learn how to use it
Then we have to make our library available at the time of compilation and execution, this is done by adding our working directory to the environment variable LD_LIBRARY_PATH so that the linker can find our library file
(for details explore the command ldconfig).
$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/my_path/
Another option is:
$ ldconfig /usr/local/lib
The ldconfig command configure dynamic linker run-time bindings, and configures them in the appropriate path where it will search as the environment variable mentioned above.
For more information about static libraries go to my older blog