Static and Dynamic. Static or Dynamic?

A complete guide into the characteristics, usage and differences between static and dynamic libraries

  • Static library. (lib) A Static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable. This executable and the process of compiling it are both known as a static build of the program. Historically, libraries could only be static.
    They are usually faster than the shared libraries because a set of commonly used object files is put into a single library executable file. One can build multiple executables without the need to recompile the file. Because it is a single file to be built, use of link commands are simpler than shared library link commands, because you specify the name of the static library.
  • Dynamic or Shared libraries are .so (or in Windows .dll, or in OS X .dylib) files.
    These are linked dynamically simply including the address of the library. Dynamic linking links the libraries at the run-time. Therefore, all the functions are in a special place in memory space, and every program can access them, without having multiple copies of them.
image from:

Why use libraries?

This is where libraries come in handy. Instead of defining functions in every file, functions can be bundled up in a library file. This file can then be called within the program once and the functions can be used as many times as needed in the program.

How do they work?

Libraries can be default by the system or user created. Either way, they have to be declared in a header file and included atop the program where the library is being used.

How to create a static library?

The first step to create a static library is to gather all the .c files we want to include in out library and place them in the same directory. Like so:

  • The rflag will replace older object files in the library, with the new object files, and the cflag tells ar to create the library if it doesn’t already exist.
  • mylibrary is the name of the library to be created with my original functions inside. This means that if I wanted to use my functions, instead of defining every function inside my code or linking each function independently, I could use them through the library.
  • <filename>.o is every name of every object file I want to include in my library.

How to use a static library?

Now that the library is created with whatever functions we wanted to include, it is now available to use by any other user and program.

  • gcc is the compilation command
  • my_program.c is the program I want to compliate
  • -L. will tell the compilator to look in this directory for library files. It opens the option that a library will be looked for.
  • -l (mind the caps!) will be followed by the name of the library we created (without the “lib” and the extension “.a”- -l will handle this info)
  • -o my_program indicates the output file name.

How to create a dynamic library?

Dynamic linking means the use of shared libraries. Shared libraries usually end with .so (short for "shared object").

  • The wildcard * tells the compiler to compile all the .o files into a dynamic library which is specified by the -shared flag.
  • The naming convention for dynamic libraries is such that each shared library name must start with lib and end with .so . In this case, “all” between lib and .so could be replaced with any desired library name.

How to use a dynamic library?

To use a dynamic library, you can run:

  • gcc is the compiler, like we already know
  • The -L flag tells the to look in the current directory for the library file.
  • 0-main.c is the source code to which the library will be linked to
  • -lmylibrary is the library name that the compiler will search for
  • And the -o flag will be followed by the desired output name, in this case mylib.

Advantages and disadvantages of static and dynamic libraries:

  • When a bug is fixed in a dynamic library, every application that references this library will profit from it. On the contrary, this also means that if the bug remains undetected, each referencing application will suffer from it (if the application uses the affected parts).
  • An advantage of using static libraries is that the application can be certain that all its libraries are present and that they are the correct version.
  • Static linking can also allow the program to be contained in a single executable file, simplifying distribution and installation.
  • In static linking, the size of the executable becomes greater than in dynamic linking, as the library code is stored within the executable rather than in separate files.
  • The program and whatever library it uses are linked with symbolic link . For dynamic libraries, in case of two or more applications using shared library, the overall size taken by library will be less.
  • In static libraries the library is already linked , so application load time is less.
  • In dynamic, application loading time is more because of dynamic library loading and dynamic link creation activity.



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