Dynamic libraries
Dynamic libraries

Dynamic libraries in C: Definition, explanation and creation

Geraldine Meneses
3 min readSep 25, 2021

Why use libraries in general and how they work in c?

Libraries in C are not unlike public libraries in cities, towns, or neighborhoods. A public library provides access to a multitude of information in various media forms to the public for access and use. Functions in a C library can be used and accessed by programmers to create several different programs.

As a programmer, you may find yourself using the same function or functions repeatedly. In this case, it is best to put this function or functions in a library to speed up the compilation of the program. C libraries store files in object code; during the linking phase of the compilation process files in object code are accessed and used. It is faster to link a function from a C library than to link object files from a separate memory sticks or discs.

compilation of libraries in c
compilation of libraries in c

How to create them (Linux only)

To make the process of creating a dynamic library, we will use the following files:

root@c698ec171c6e:/holberton/dynamic_libraries# ls
0-isupper.c 0-strcat.c 1-isdigit.c 1-strncat.c 2-strlen.c 3-islower.c 3-strcmp.c 4-isalpha.c 5-strstr.c 9-strcpy.c
0-memset.c 100-atoi.c 1-memcpy.c 2-strchr.c 2-strncpy.c 3-puts.c 3-strspn.c 4-strpbrk.c 6-abs.c _putchar.c

To get started, we must first create the object files with the command gcc -fPIC -c *.c

root@c698ec171c6e:/holberton/dynamic_libraries# gcc -fPIC -c *.c
root@c698ec171c6e:/holberton/dynamic_libraries# ls *.o
0-isupper.o 0-memset.o 100-atoi.o 1-memcpy.o 2-strchr.o 2-strncpy.o 3-puts.o 3-strspn.o 4-strpbrk.o 6-abs.o mat.o
0-main.o 0-strcat.o 1-isdigit.o 1-strncat.o 2-strlen.o 3-islower.o 3-strcmp.o 4-isalpha.o 5-strstr.o 9-strcpy.o _putchar.o

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

In the next step we will create the library called libdynamic.so:

root@c698ec171c6e:/holberton/dynamic_libraries# gcc -shared -Wl, -soname , libdynamic.so -o libdynamic.so * .o

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

root@c698ec171c6e:/holberton/dynamic_libraries# ls *.so
libdynamic.so

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

Static vs Dynamic

The concept of a library is powerful because it allows 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.

--

--

No responses yet