Dynamic libraries in C: creating something on what the others will rely.

This is the third article in a series about what the compilation process is and how it works, using the C language as an example.

  1. Creating new worlds with GNU Compiler Collection.
  2. The power of a book, or how to use static libraries in C.
  3. Dynamic libraries in C: creating something on what the others will rely. (this article)

***

As we’ve mentioned in the previous article, one of the problems with computer programs, is that they have tendency to grow larger and larger, bringing more challenges for its creators and especially maintainers and increasing time of the compilation and linking.

We already know that the mechanism that addresses this issue in C is a library, as well as we know what static libraries are. So today we will focus on the dynamic libraries, and will answer the following questions: what is a dynamic library and how it is different from static one, how does it work, how to create and how to use it.

0. Static vs Dynamic

The concept of a library is powerfull because it allaws an end user to link functions via their object file’s during either run-time or compile time — this differs depending on the type of library you create.

Static library is a collection of object files, while dynamic or shared library is a collection of functions compiled and stored in an executable with purpose of being linked by other programs at run-time.

Dynamic libraries provide a means to use code that can be loaded anywhere in the memory. Once loaded, the library code can be used by any number of programs. This way the size of programs using dynamic library and the memory footprint can be kept low as a lot of code is kept common in form of a shared library.

1. How to create the dynamic library

To demonstrate how to create the dynamic library we will use the codebase of the project form the previous article:

To start we need to create the object files first with command gcc -fPIC -c *.c

As you have noticed, this time we created the object files with the -fPIC flag. This flag stands for Position Independent Code, a characteristic required by shared libraries.

On the next step we will create the library named

gcc -shared -Wl,-soname,libtools.so -o libtools.so *.o

The -shared key tells the compiler to produce a shared object which can then be linked with other objects to form an executable. -Wl flag passes an options to linker with following format -Wl,options, in case of our example it sets the name of library, as it will be passed to the linker.

As the result, we’ve got the library that is ready to be used.

2. How to use a dynamic library

Because of its purpose, our library has to be shared dynamically during the linking with other programs, and to make it happen, we have add a path to the library to the LD_LIBRARY_PATH environment variable:

export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

In case of our example, it is the current working directory, and we can use the . to add its path. Now the operating system is aware of where to look if some program will request a functionality from the library. Alternatively to configure dynamic linker run-time bindings, we can use the ldconfig command with the -n and -L flags.

And the last but not least: a program that wants to rely on the functionality of our library must be compiled in the following manner:

gcc our_sources.c -L. -ltools -o resulted_program

where -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. Please note that we didn’t provide the lib prefix, as well as the .so extension: they were resolved by the compiler.

As we can see, our program compiles and executes.

3. Tools to work with dynamic libraries

If we need to find out what functions a library has, we should use the nm command:

nm libtools.so

To list the dependencies of a library, the ldd command should be used:

That’s all, folks!

Summary:

In this article we recalled the reasons why such a concept as dynamic library exists, how it is different from the static library, how it works, how to create and how to use it.