Static libraries vs Dynamic libraries in C

Process of linking from various libraries to get the executable file.

Why use libraries?

  • .a for Static Libraries.
  • .so for Shared Libraries.

How to create a Static library

  1. Create a C file or multiple C files that contains a function in it: In this example, I’m going to use a simple function _strlen.c that gives the number of characters in a string.

How to create a Dynamic Library

  1. Like in the static libraries, create a C file that contains a function in it:

How to use the libraries:

Differences Between Static and Dynamic Library

What are the advantages and drawbacks of each of them?

There are certain advantages when you use static libraries, some of them are:

  • You can take your program to another computer and it will execute normally. It’s Portable!
  • If you do changes into the static library this won’t affect the executable. This makes the Static libraries a good option when your programs aren’t so big and kind of simple.
  • All of the objects are in the executable file, making it quite big, especially if you want to add more and more functions. So if the program gets complicated this might be not the best choice library to use.
  • If a library code is updated, the programmer has to recompile the program into a new executable file.

On the other hand, the advantages when you use Dynamic libraries are:

  • It only needs one copy at runtime. This facility makes it a flexible library that can be used in multiple programs without the need for each file of having its own copy.
  • I a library is updated, the executable files don’t need to be recompiled again.
  • It is dependent on the library, for example, if the library is erased the executable file won’t work.
  • Again, if the library suffers a problem, like corruption, the executable cannot work.

References:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Valentina Jaramillo Ramirez

Valentina Jaramillo Ramirez

EN | SP Software Developer Student at Holberton School in Medellin, Colombia.