The differences between static and dynamic libraries.

Either write something worth reading or do something worth writing

Andrés Felipe García Rendón
3 min readDec 16, 2019

Basically, a library is a collection of functions.

  • Why using libraries in general?

It is important to use libraries in c because it allows us to activate the use of several functions by making a single call from the .head file

  • How do they work?

You may have noticed that we are using functions which are not defined in our code, or in that particular file. To have access to them, we include a header file, that contains declarations of those functions (file .h). After compile, there is a process called linking, that links those function declarations with their definitions, which are in another file. The result of this is the actual executable file.

Now, the linking as I described it is a static linking. This means that every executable file contains in it every library (collection of functions) that it needs. This is a waste of space, as there are many programs that may need the same functions. In this case, in memory there would be more copies of the same function. Dynamic linking prevents this, by linking at the run-time, not at the compile time. This means that all the functions are in a special memory space and every program can access them, without having multiple copies of them. This reduces the amount of memory required.

As I mentioned at the beginning of my answer, this is a very simplified summary to give you a basic understanding. I strongly suggest you study more on this topic.

The static library is a .lib file that will be linked inside your executable and won’t change with time.

The dynamic library is a .dll file linked to your executable and may change depending on the dll file you load when you execute it.

  • How to create them (Linux only)?

To create a library we must first have all the .c files with the functions that we want our library to have. After this, we create an .h file with all the prototypes of the functions. Then we execute the following commands and we have already created our library:

  • gcc -fPIC -c *.c
  • gcc -shared -o (name of my library).so *.o
  • Why using libraries in general?

It is important to use libraries in c because it allows us to activate the use of several functions by making a single call from the .head file

Differences between static and dynamic libraries and What are the advantages and drawbacks of each of them:

--

--

Andrés Felipe García Rendón

Web Developer graduated from Holberton School. With experience in as C, Python, NodeJS, React, React Native, HTML, CSS, Boostrap and Flexbox