Static and dynamic libraries in C language

Daniel V.
4 min readSep 7, 2020

As we develop some code, we realize that some parts of it are used several times, at that moment we realize how great it would be to be able to put those functions in a separate directory from the specific programs and have them already compiled, so way that we can use them whenever we want

Why using libraries in general

The advantages that implementing this would bring would be:

  • Not having to rewrite the code (or copy-paste).
  • We will save the time of compiling each time that code is already compiled.
  • The already compiled code will be tested and reliable.

The way to do this is to make libraries. A library is one or more functions that we have already compiled and prepared to be used in any program that we make. You have to have enough eye when we do them so as not to put any dependency on something specific in our program.

How do they work, How to create them?

To put our code in a library, we need to organize it in the following way:

  • One or more source files .c with the code of our functions.
Image 1. Source files
  • One or more .h header files with the typedefs, structs, and so on, and prototypes of the functions that we want to be able to use.
Image 2. Header File

And now we can create our dynamic Library with the following command:

gcc *.c -c -fpic

This command generates one object file .o for each source file .c.

The flag -c is to compile and assemble, but do not link

The -fpic flag is to indicate that the code is position-independent, which means that it does not matter where the code stores the code in memory.

After writing the next command:

gcc -shared -o liball.so *.o

With this command, we compile all .o files into a dynamic library that is specified with the -shared flag in a file called liball.so.

Finally, we’ll need to export the path for libraries so that programs know where to look for them by executing the following command:

export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH

How to use them

With the following command we can compile our code:

gcc -Wall -pedantic -Werror -Wextra -L. 0-main.c -lall -o len

If we have a source file called 0-main.c

#include "holberton.h"
#include <stdio.h>
/**
* main - check the code for Holberton School students.
*
* Return: Always EXIT_SUCCESS.
*/
int main(void)
{
printf("%d\n", _strlen("Holberton"));
return (EXIT_SUCCESS);
}

and executing the command we have as output an executable file called len will be generated, and executing this file obviously with the permission of execution with the command ./len we will have completed our process and the program will run smoothly.

Differences between static and dynamic libraries

A static library is a library that is “copied” into our program when we compile it. Once we have the executable of our program, the library is useless (in other words, it is used for other future projects). We could delete it and our program would continue working, since it has a copy of everything it needs. Only that part of the library that is needed is copied. For example, if the library has two functions and our program only calls one, only that function is copied.

A dynamic library is NOT copied into our program when compiling it. When we have our executable and we are executing it, every time the code needs something from the library, it will look for it. If we delete the library, our program will give an error that it cannot be found.

Advantages and drawbacks of each of them

  • A program compiled with static libraries is larger, since everything it needs is copied.
  • A program compiled with static libraries can be taken to another computer without having to take the libraries.
  • A program compiled with static libraries is, in principle, faster in execution. When you call a library function, you have it in your code and you don’t have to go read the dynamic library file to find the function and execute it.
  • If we change a static library, the executables are not affected. If we change a dynamic, the executables are affected. This is an advantage if we have changed the library to correct an error (it is corrected automatically in all executables), but it is inconvenient if touching that makes us change the executables (for example, we have added one more parameter to a library function , ready-made executables stop working).

And that’s it, I hope this article has been helpful!

Have a nice day!

--

--