Differences between static and dynamic libraries

Cristian Fayber Pinzon Capera
5 min readMay 4, 2021

All you need to know about this…

Why using libraries in general?

Many times we find that the same portion of code is used in different parts, which is not a good practice, mainly we refer to function codes that serve as solutions for various situations in logic. That is why the importance of creating libraries where these functions are stored and can be used as if they were taken from a toolbox.

Now you’ll know as are libraries of code, taking as an example the C language.

How do they work?

Libraries have the mission of providing not only functions, but also structures, macros and other definitions in general, what is different is how each one is handled with respect to our already compiled programs, which we will show the process below.

How to create them (Linux only)

In order to put our code in a library, we need to organize it as follows:

  • One or more source files .c with the code of our functions.
  • One or more .h header files with the types (typedefs, structs and enums) and prototypes of the functions that we want to be able to use.

We show you an example of how our header file(.h) could look with the protoptypes of various functions:

As we can see, we have defined our functions which, in turn, are stored in independent source files(.C). We cannot forget to define the corresponding tags to define our header with #ifndef, #define and its closure with #endif

We must have the code corresponding to the functions within the source files (.c) for the next step, which is the creation of the object files (.o) with the flag -c for the compilation with gcc.

gcc -Wall -pedantic -Werror -Wextra -c *.c ;

With the above command, we create object files from each source file present in our current directory. The following steps change for dynamic and static libraries to finally see the result of each one with their respective differences.

Create a static library

We will execute the ar command with the following parameters to generate a file with an .a extension that corresponds to our created library

ar -rc <name of library file->.a *.o

In this case, the library file name is libholbertonschool.a (including its extension) and the second command displays all included object files in the terminal.

Create a dynamic library

We will execute the ld command with the -o flag followed by the object files and finally the -shared flag

ld -o <-name of library file->.so *.o -shared

With this, we have our library file and your extension is .so, their contain we can see with the nm command with the flags specified at the image above.

Here we need an additional step to be able to use it and that is to tell the program, while it is running, where the dynamic libraries are, since it will go to look for them every time a function of them is called. We have to define the environment variable LD_LIBRARY_PATH, in which we put all the directories where there are dynamic libraries of interest.

If we do not do this, the executable that we are going to use something from our dynamic library cannot access it, in the second line where is the name of the library file with .so extension, the message “not found” would then appear.

What are the differences between static and dynamic libraries?

  • A static library is a library that “is copied” into our program when we compile it, a dynamic library is NOT copied into our program when we compile it.
  • A static library once we have the executable of our program, the library is useless, a dynamic library every time the code needs something from the library, it will go to find it. If we delete the library, our program will give an error that it cannot be found.

What are the 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).

I hope this information has helped you, we will see you in a next publication

--

--