Static vs Dynamic libraries.

  • One or more .h header files containing the types (typedefs, structs) and prototypes of the functions we want to have available for utilization.
library1.cint sum (int a, int b)
return a+b;
int sub (int a, int b)
return a-b;
library1.h#ifndef _LIBRARY_1_H
#define _LIBRARY_1_H
int sum (int a, int b);
int sub (int a, int b);
The Uruguay’s Congress library. Totally unrelated to this article subject but it looks cool.
  • A program compiled with static libraries can even be moved over to a different computer without the need to move the libraries along with it. In static libraries, once everything is bundled into your application, you don’t have to worry that the client will have the right library (and version) available on their system.
  • A program compiled with static libraries is (at least on paper) faster on its execution. When it calls a function from the library, it is actually on its own code. Therefore, there is no need to access the library file to retrieve information from it.
  • One drawback of static libraries is, for any change in the static libraries, you have to recompile the main program every time.
  • If we change or even remove a static library, already created executables are not affected. However, if we change a dynamic library, executable will be affected. This could be an advantage, if we have amended the library in order to correct an error (as the error will be fixed in all executables).
  • Dynamic libraries will conserve memory since each program can access the library without creating an individual copy of the functions.

Creating static libraries using GNU.

Let’s assume we have a (very) simple C program called lib_mylib.c:

#include <stdio.h> 
void fun(void)
printf("fun() called from a static library");
#ifndef LIB_MYLIB_H
void fun(void);#endif
gcc -c lib_mylib.c -o lib_mylib.o
ar rcs lib_mylib.a lib_mylib.o
#include "lib_mylib.h"void main(){
gcc -c driver.c -o driver.o
gcc -o driver driver.o -L. -l_mylib
fun() called from a static library

Creating dynamic libraries using GNU.

The process starts in a similar way than before, by creating the .o files out of the source .c files. That is done quick and easy with the following command:

gcc *.c -c -fPIC
gcc *.o -shared -o
$ nm -D
0000000000000b1c T _abs
0000000000000b4f T _atoi
0000000000202048 B __bss_start
w __cxa_finalize
0000000000202048 D _edata
0000000000202050 B _end
0000000000000bf4 T _fini
w __gmon_start__
0000000000000910 T _init
0000000000000b13 T _isalpha
0000000000000b2e T _isdigit
0000000000000b0a T _islower
0000000000000b25 T _isupper
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
w _Jv_RegisterClasses
0000000000000a75 T main
0000000000000ba9 T _memcpy
0000000000000b97 T _memset
U printf
0000000000000ae0 T _putchar
0000000000000b37 T _puts
0000000000000b59 T _strcat
0000000000000bba T _strchr
0000000000000b89 T _strcmp
0000000000000b41 T _strcpy
0000000000000a9f T _strlen
0000000000000b67 T _strncat
0000000000000b78 T _strncpy
0000000000000bd7 T _strpbrk
0000000000000bc9 T _strspn
0000000000000be5 T _strstr
U write
$ ldd len => (0x00007ffd391b8000) => /home/vagrant/holbertonschool-low_level_programming/0x18-dynamic_libraries/ (0x00007f91da758000) => /lib/x86_64-linux-gnu/ (0x00007f91da388000)
/lib64/ (0x00007f91da960000)



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