C dynamic libraries.

Static and dynamic libraries

Lina María Montaño Ramírez
3 min readDec 17, 2019

I recently made a post about static C libraries. Returning to this we’ll talk about dynamic C libraries.

Dynamic

When dynamic libraries are linked, no library code is included directly in the linked destination. Instead, libraries are loaded into memory at runtime before symbols are resolved. Because the code is not statically bound in the executable binary, there are some advantages to loading at runtime. Mainly, libraries can be updated with new features or bug fixes without having to recompile and re-link the executable. In addition, being runtime loaded means that individual code libraries can have their own initializers and clean up after their own tasks before being downloaded from memory.

Libraries

Dynamic libraries are a type of Mach-O binary1 that is loaded at the startup or runtime of an application. Since the executable code of a dynamic library is not statically linked to the target executable, this offers some advantages when you need to reuse the same code.

How do we create a dynamic library?

Let’s create a dynamic library using all the *.c files in our current directory. First we need to compile our C files into object code and make them position independent. We can do that using:

$ gcc -Wall -fPIC -c *.c
This command generates one object file *.o for each source file *.c .

The -shared flag tells the linker to create a special file called a shared library. The -fpic option converts absolute addresses to relative addresses, which allows for different processes to load the library at different virtual addresses and share memory.
With the -c option, the *.o file is not linked.

$ gcc -shared -o filename.so *.o

To finish this process, we export the path of the libraries using the following command:

$ export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH

Static

A diferencia de las dinámicas, la vinculación de bibliotecas estáticas incluye el código de archivo objeto de la biblioteca en el binario del destino. Esto resulta en un mayor tamaño en el disco y tiempos de lanzamiento más lentos. Dado que el código de la biblioteca se añade directamente al binario del destino vinculado, significa que para actualizar cualquier código de la biblioteca, el destino vinculado también tendrá que ser reconstruido.

Libraries

A static library is a container for a set of object files. Static libraries use the file extension “.a”, which comes from the (ar)chive type. An archive file was designed to contain a collection of files. This is ideal for the transport and use of many object files that comprise a single code library.

Using Dynamic Libraries (Linux only)

the use of the ‘-L’ flag — this flag tells the linker that libraries can be found in the given directory (‘.’, referring to the current directory), in addition to the standard locations where the compiler searches for system libraries.

$ gcc isupper.c -L. -lholberton -o isupper

The difference is that static libraries, although they can be reused in several programs, are locked in one program at compile time; whereas dynamic or shared libraries exist as separate files outside the executable file and are not so often locked in their execution.

https://www.geeksforgeeks.org/difference-between-static-and-shared-libraries/

--

--

Lina María Montaño Ramírez

Backend Developer 👩‍🏫 Mentor @coderiseorg 🎧 Podcaster en @caminodev ! 💚 Organizer @node_co