All about Libraries in C language
At the time of programming we find a series of features that we have the need to repeat constantly. Programming in C these features can be grouped into dynamic or static libraries, which we can access to avoid duplicate code, increasing the quality and development time, although they share the same generic name “library”, they use different mechanisms to provide their functionality to the executable.
About the dynamic libraries I was talking about in my post of October 11, 2020, which you can access here.
Today I will explain about the dynamic libraries, but first let’s refresh our memory.
What are libraries?
They are pieces of code that contain within them pre-built variables and functions that can be used by an executable.
Dynamic libraries: in few words, we can say they are files that contain object code built independently of their location. They are to be required and loaded at runtime by any program though, instead of having to be linked, previously at the time of compilation.
Static libraries: they are the first ones created when programming languages started to be developed, formerly they were one of a kind. A static library, is a file that has several object code files packaged and at the time of compiling will be copied into the executable file. Unlike the dynamic libraries, they will remain within the executable file.
How to create and use Static Libraries
(Linux only — Cloned content of the reference post)
The first thing we must do is transform our functions into objects, for which we use gcc with the -c parameter.
Next step, is to create the static library using the program “ar”, its name is an acronym for “archiver”.
The ‘c’ flag tells ar to create the library if it doesn’t already exist.
The ‘r’ flag tells it to replace older object files in the library, with the new object files.
Using the -t parameter in “ar” we check if the library has been created and what is its content.
After the library is created it must be indexed using the command ranlib. This is important because later it will be used by the compiler to speed up the search for symbols within the library, and to make sure that the order of symbols does not slow down the compilation proccess.
Finally, to compile our main, we must specify to gcc which is the name of our library. For that we use the -L parameter followed by the name of the static library we have.
How to create and use them Dynamic Libraries
To do this is simple, you can follow these steps
Imagine a file “my_library.c” that has this content:
A file called “main.c” which will test the dynamic library
And the “header.h” with this
To be able to create the dynamic library, first we must pass our file “my_library” to object. We do it in the following way
It should be remembered that if we have several
.c files we can use the
*.c wildcard to include all files with this extension, avoiding detailing each one of them (
gcc -fpic *.c -c).
Now that we have it created, the compiler will be able to identify it, looking for files that begin with
"lib" and end with a library extension (
.so for dynamic,
.a for static)
Our library is ready to be incorporated into the system and we must export it
It’s time to compile our test file and see how our new library works.
$ gcc test_function.c -o test_function
And we run it!
When function is called this line is printed!
Other commands you need to know
ddl : A command in Linux to display the shared libraries required by any given program. To use it you must put the obsolute path where your executable is.
$ ldd /bin/ls
libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f3f561e3000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f3f56022000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f3f55fae000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f3f55fa9000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f3f55f88000)
ldconfig : examines the existing files and creates the sonames as symbolic links to the real names, as well as setting up the cache file /etc/ld.so.cache.
If there are any doubts, leave them in the comments, thanks for going all the way.