In C, a library is a file containing several object files, that can be used as a single entity in a linking phase of a program. There are two types of libraries used in this way. The first is called a static library. The second is called a dynamic(or shared) library, it’s that one we’re going to take a closer look at today.
By using libraries, we can save time by reusing our own work (or even someone else’s), for instance, we can collect some functions we use on a regular basis inside a library, so we can use said functions whenever we need to.
Differences and Advantages
The way dynamic libraries work is that they are referenced as the program is running. This means there’s a dependency upon the application and the library. In a static library, all of the code within the library is copied over to the application. So in a static library, the library file becomes irrelevant once it has been copied, making the application completely independent, but resulting in larger binary files, and needing more space on disk and main memory.
A dynamic library is great because you don’t need to recompile or relink the applications if you end up changing how some functions work, as you would need to do in a static library. Shared libraries can also be called by programs written in different programming languages. On the other hand, using a shared library means that your application is dependent on tons of files that aren’t local to your individual program. This means you have to ensure everything’s updated and in sync, and that if a dynamic library is corrupted, or there’s some other issue with it, the application will not run.
Let us now take a look at how to create a dynamic library;
Put all your source files (.c) into one directory, including the header file containing the prototypes for the relevant functions.
Then simply use this command to turn all of those files into a dynamic library;
gcc -g -fPIC -shared *.c
Let’s take a close look at the flags.
- -g → used to includes debugging information
- -fPIC → “Position Independent code”; this allows for the code to be located at any virtual address at runtime
- -shared → creates the shared library with the prefix lib and the suffix .so, standing for “shared object”
If you want to know how to create a static library, you can take a look at my previous medium post on the subject here.
To use a dynamic library, a path must be added to the library to the environment variable LD_LIBRARY_PATH. To do this, we use the command:
This command will load the variable LD_LIBRARY_PATH with our dynamic library in the current directory. If we miss this command, the system won’t know where the library is located and the will show an error message.
Finally, to compile a program with the library, we must use the following command;
gcc exemple_C_files.c -L. -ldynamic
The -L flag specifies the path to the library, in our case it is current directory, and -l flag specifies the name of the library to use.
Our program is now ready to use, and we can update the library at any time without needing to recompile it.