DIFFERENCES BETWEEN STATIC AND DYNAMIC LIBRARIES

Liz Victoria
3 min readMay 5, 2020

¿Why using libraries in general?

When we create a program sometimes we need need to use several functions in different places. So we want to save those functions in a different place and in this way we can use them many times during the program run.

The way to do this is by making libraries. A library is one or more functions that we already have compiled and are ready to be used in any program we do. We must very careful when we create them so don’t put any dependency on something specific of our program.

How to create them

Static Libraries

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

  • One or more .c source files with the code of our functions.
  • One or more .h header files with the prototypes of the functions that we want to used.

In this image we can to see the files with .c extension and then is the file holberton.h

Now we can to create the library and for this we have to run the next command ar -rc libholbertonschool.a *.o

AR: With the command ar we can keep a group of file like a single file.

RC: The c flag tells ar to create the library if it doesn’t already exist. The r flag tells it to replace older object files in the library, with the new object files.

libholbertonschool.a is the name of the library.

  • .o this command selects all files with extension .c and this way we can use all the functions that we have before in multiple files like one single.

Dynamic Libraries

To compile the same files but as dynamic libraries we have that follow the next steps:

  • Compile the source, same that before and this way take the objects.
  • We have to create the object file and for this, we can use this command gcc -fPIC -c *.c. This flag stands for position independent code so it will generate an object file and this file is position independent similar
  • Create the library with the command ls. The options for this command will be ls -o liball.so *.o -share file1.o file2.o file3.o and this way take all the files with extension .o. liball.so indicate the name that we want to give it to the library. The option -shared indicates that it is a library and not an executable (option for default) and the file1.o, file2.o is the file that we want a push in the library.

Where nm means that we will be create a library dynamic and the extension .so mean that is a file from dynamic library.

Difference 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 (that is, it is useful for other future projects). We could delete it and our program would continue to work since it has a copy of everything we need. 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 to 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 display an error message saying the library cannot be found it.

--

--