Usefulness of C libraries: a look at the creation of dynamic libraries.
Diana Parra
Backend developer at Capital Lab by Credicorp Capital | Degree in natural sciences
We know the importance of libraries in a city or in a school, but really, the question I will try to solve in this blog is why use libraries in C?
Libraries are a tool for collaboration
The subtitle of this section is quite defining. Libraries are an indispensable tool and are basically pre-existing code that is compiled and ready to use. I will post an example to find a simple and authentic answer about the use of libraries in C.
Imagine that you are working on a very large software project (together with other developers, of course) of which you will logically have a large set of functions, what to do with them if in the future I want to reuse them for another project (or even within the same one) Creating a library would be the best option, since you would no longer have to copy the source code of each function and compile it again.
The libraries employ a software design called "shared components" or "file libraries" that bundles several object code files compiled into a single file.
How libraries work: creation and use
In a previous blog, I discussed the creation of static libraries, so click on the link for information about this type of libraries. In this blog I will go deeper into the creation and use of dynamic libraries.
In C there are two types of libraries that can be created: static libraries (.a extension) or dynamic libraries (.so extension). The biggest difference is that static libraries are linked BEFORE execution, that is to say, they are statically linked to a program at compile time and dynamic libraries are only used DURING runtime (see image at the end of the blog for a more detailed definition of each type of library as well as the advantages and disadvantages of each).
When we write in C and compile it (see blog about the compilation phases that I have written) the source code (.c extension) is converted into object files (.o extension) in machine language. To run these programs we have to generate an executable and for this we use the linker.
Note that the scope of the compiler is restricted to a single file called header file (it can be called header.h) that contains all the function prototypes (with their corresponding data types and arguments) that are necessary for a C program to run properly.
Now that we know grosso modo the functionality of the libraries we will see how the dynamic libraries are created and used.
Step-by-step creation and use of dynamic libraries (UNIX O.S. only)
Creating dynamic libraries consists of only two steps:
- Create object code
Compile the source code (all files with .c extension) with the following command:
gcc -c -Wall -Werror -fpic *.c
The -fpic flag is a feature required by shared libraries. It is a compiler (GNU) directive to generate position-independent code (more information: here.
Once the object files (.o extension) have been created...
2. Create the library
gcc -shared -o name_library *.o
The -shared flag produces a shared object that can then be linked with other objects to form an executable.
Once the library is created, we can use it. There are several ways to tell our compiler where the shared library is located so that it is available at runtime (see links below for more information), however, I will only talk about the use of the environment variable LD_LIBRARY_PATH for this we use the command:
export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
Using this environment variable, you can define where to look for available shared objects.
The above image contains an entry point (main() function) that will be compiled with the newly created library. The -L flag indicates the directory in which the created library is located and the -l flag indicates to the compiler the name of the library. The use of the ldd len command is to list the shared library dependencies of the executable. We can notice that after setting the library path to the environment variable, the runtime dependency was corrected. We can then run the program, which in this case uses the _strlen function to display the length of the word "Holberton".
More information about the library -> We can obtain more information about it, using the command:
nm -D name_library
The nm command lists the symbols contained in the shared library or can also be used to list the symbols contained in the object files (see man nm).
Overview of the types of libraries
In the following image you will find everything related to the two types of libraries that I have discussed in this blog, as well as their advantages and drawbacks. Click here to see image in better quality.
For more information, click on the following recommended links:
- Shared libraries with GCC on Linux: https://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html
- Static, Shared Dynamic and Loadable Linux Libraries: https://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html
- Anatomy of Linux dynamic libraries: https://developer.ibm.com/technologies/linux/tutorials/l-dynamic-libraries/
- Overview of Dynamic Libraries: https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/OverviewOfDynamicLibraries.html