Static Libraries vs. Dynamic Libraries in C

What is a Library in C?

How libraries work?

What is a Static library?

How to create a Static library?

  • One or more source files .c with the code of our functions. To see our files run ls -l *.c.
  • One or more .h header files with the types and prototypes of the functions that we want to be able to use. In this case our header file is called holberton.h.

How to use the Static library?

#include "holberton.h"int main(void)
{
_puts("Programming is my passion - Lex");
return (0);
}

What is a Dynamic library?

How to create a Dynamic library?

  • One or more source files .c with the code of our functions. To see our files run ls -l *.c.
  • One or more .h header files with the types and prototypes of the functions that we want to be able to use. In this case our header file is called holberton.h.

How to use the Dynamic library?

Differences between static and dynamic libraries

  1. Linking time

Advantages and Disadvantages of Dynamic Libraries

  1. It only needs one copy at runtime. It is dependent on the application and the library being closely available to each other.
  2. Multiple running applications use the same library without the need of each file having its own copy.
  3. However, what if the dynamic library becomes corrupt? The executable file may not work because it lives outside of the executable and is vulnerable to breaking.
  4. They hold smaller files.
  5. Dynamic libraries are linked at run-time. It does not require recompilation and relinking when the programmer makes a change.

Advantages and Disadvantages of Static Libraries

  1. Static libraries resist vulnerability because it lives inside the executable file.
  2. The speed at run-time occurs faster because its object code (binary) is in the executable file. Thus, calls made to the functions get executed quicker. Remember, the dynamic library lives outside of the executable, so calls would be made from the outside of the executable.
  3. Changes made to the files and program require relinking and recompilation.
  4. File size is much larger.

--

--

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