Obtained from: https://cdn.asiatatler.com/asiatatler/i/ph/2020/04/14145432-valdemaras-d-7vpfyhb-j8y-unsplash_cover_1920x1217.jpg

Custom Libraries In C Part 2: Dynamic Libraries (Linux):

--

If you haven’t read the first part, which talks about static libraries, I invite you to read it to understand a little more about the custom libraries in c and how to create a static library:
https://2348.medium.com/custom-standard-libraries-in-c-be4ad413632f

We have already talked about static libraries, but there is another type of libraries: dynamic or shared libraries; these consists of routines that are loaded into your application at run time, that means that when you compile a program that uses dynamic libraries it does not become part of the executable, which allows that the library can be used by many programs and that you can modify the functions that they contain without needing to recompile the executables that use it.

How does it work?

The C libraries are used in the linking stage, dynamic linking doesn’t require the code to be copied, it is done by just placing the name of the library in the binary file. The actual linking happens when the program is run, when both the binary file and the library are in memory. Examples of Dynamic libraries (libraries which are linked at run-time) are .so in Linux and .dll in Windows.

How to create a shared library in Linux?

As in the static libraries, the first thing to do is to get the object files from the source code, for this we will assume that we want to get the dynamic library libhelloworld.so from the source files hello.c and world.c, for this we use the following command:

gcc -c -fPIC hello.c world.c

This will generate the object files . o from the source code files .c, the -c flag allows to obtain the file before the linking stage and the -fPIC is used to make the code position-independent, which makes it not matter in which memory position the code is loaded, which is necessary for it to work in certain operating systems and processors, with this we would obtain the following files: hello.o and world.o
Both types of libraries are compiled from the object files, so once they are obtained we can use the following command:

gcc hello.o world.o -shared -o libhelloworld.so

To generate the compiled library libhelloworld.so. The convention for naming libraries is that they start with the lib prefix and have a .so extension, the -shared flag tells the compiler that we want to create a dynamic library.
We also have to ensure that the compiler knows where the library is when it is used, for this we have to export the directory where the library is located to the environment variable LD_LIBRARY_PATH, we do this with the following command:

export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH

How to use a Dynamic library?

To create an executable myexe.out from a source code file main.c that uses functions from the library we just created, we use the following command:

gcc -L. main.c -lhelloworld -o myexe.out

The -L flag tells the compiler to look in the current directory for the library file and the name just after -l flag must be the name of the library we want to link without the lib prefix, in this case, as we want to link the library libhelloworld.so we use the option -lhelloworld, the -o flag allows to define the name that will have the executable, in this case we will get an executable myexe.out.
Now that you know a little more about custom static and dynamic libraries in C, I advise you to go and replicate what you have learned and share the knowledge!

--

--