Understanding Dynamic Libraries: What They Are and How They Work

Learn how to combine multiple C object files into single executable with dynamic libraries.

Dynamic libraries are a powerful tool in programming that can improve the performance, modularity, and maintainability of a program. In this article, we will explore what dynamic libraries are, how they work, and why they are useful.

When a programmer writes a program, they typically write the source code in one or more source files. At some point, these files must be compiled into a single executable. You can do this by creating a library.

A library is a collection of pre-written code that can be used by a programmer to implement common functions or features in their program. By using a library such as static or dynamic libraries, a programmer can better organize and manage the code, which can make the program easier to maintain and develop.

Dynamic libraries, also known as shared libraries or DLLs, are a type of library that is not linked with a program at compile time but is instead loaded at runtime. Unlike static libraries, which are linked with the program at compile time, dynamic libraries are stored in a separate file and are loaded at runtime, when the program needs to access the functions or other code in the library.

This allows programs to use the code in the library without having to be statically linked to it. The dynamic library also reduces the size of the final executable file. Instead of having a redundant copy of the library, an application using a library includes only the name of the library when the final executable is created. They can also be used to provide functionality that can be updated or changed without requiring the programs that use the library to be recompiled.

Another benefit of dynamic libraries is that they can be shared by multiple programs. This means that multiple programs can access the same library code, which can save memory and improve performance.

Let us cut the chatter and get to creating a dynamic library.

Creating a shared object file (Linux)

To create a dynamic library on Linux, you will need to use a compiler toolchain that includes the GNU Compiler Collection (GCC) or another compatible compiler. You can then use the gcc command to compile your code into a shared object file, which is the Linux equivalent of a DLL file on Windows.

Step 1

Compile each of the source files into object files using the gcc command with the -c and -fPIC flags. For example:

gcc -Wall -fPIC -c file1.c file2.c ...

The -Wall flag tells the compiler to enable all warning messages, which can help you identify and fix potential issues in your code. The -fPIC flag stands for "position-independent code". Position-independent code is code that can be executed from any memory address, so it is necessary for creating shared libraries that can be used by multiple programs. While the -c flag tells the compiler to stop after compiling the source code into object code .o.

The above command will produce object files called file1.o, file2.o, …respectively.

Step 2

Use the gcc command with the -shared and -o flags to link the object files into a shared object file. For example:

gcc -shared -o libmylibrary.so file1.o file2.o ...

This will produce a shared object file called libmylibrary.so. The -shared flag tells the compiler to create a shared object file while the -o flag specifies the name of the output file.

The “lib” prefix in libmylibrary.so is used to indicate that the file is a library, and the “.so” suffix stands for “shared object”. Other than that, let your imagination run free when considering names for your dynamic libraries. This naming convention is used by the gcc command and other tools to automatically recognize dynamic libraries and link them to programs at compile time.

Step 3

After creating the dynamic library, the next step is to export the path to the library so that the linker can find it when linking it to other programs. This can be done using the export command and the LD_LIBRARY_PATH environment variable.

export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

This sets the LD_LIBRARY_PATH environment variable to the current directory followed by the existing value of the variable. This allows the linker to find shared libraries.

Using Dynamic Libraries (Linux)

To use the shared object file in another program, you can link it to the program at compile time using the gcc command with the -L and -l flags. For example:

gcc -o myprogram myprogram.c -L. -lmylibrary

This will compile myprogram.c into an executable called myprogram, and link the libmylibrary.so shared object file to the program. The -L flag tells the compiler where to look for the shared object file, and the -l flag specifies the name of the shared object file (without the lib prefix and the .so suffix).

You can then run the program using the ./ command, like this:

./myprogram

This will run the myprogram executable, which has access to the functions and data defined in the libmylibrary.so shared object file. For instance, if myprogram.c was the following:

#include "alx.h"

int main(void)
{
      puts("Hello World!");
      return (0);
}

Executing myprogram like so: ./myprogram would give us the following output: Hello World!.

And that’s it, I hope this was helpful and please let me know what you think in the comment below

If you found this article helpful, kindly follow for more on any of my social media handles:

🔗 Twitter 🔗 GitHub 🔗 LinkedIn