Dynamic (shared) vs. Static Library Performance

Connor Brereton
4 min readDec 11, 2018
Trinity College — Dublin, Ireland

Table of Contents:

  • What is a shared library?
  • How to create a shared library?
  • Why are they useful?
  • How is it different than a static library?

So what are they?

A shared library is a library that dynamically links during compilation when a user compiles a .c file. Static and dynamic linking are two processes of collecting and combining multiple object files in order to create a single executable file. The main difference between the two is the type of linking they do when creating an executable file.

Contrary to a static library, a dynamic library performs the linking process as a program is executed in Linux. Moreover, dynamic libraries are loaded into memory by programs when they are executed. During compilation, a shared libraries machine code is cached locally and has a version control of sorts that keeps track of what the most recent changes are to the file.

Let’s say you create a shared library and then run some tests. Next, you decide to add some more code to your functions in the shared library. Well, when you recompile, the compilation process just appends the recent changes to the cache versus having to recompile everything all over again as you would in a static library creation.


How to create a shared library?

So lets say we have a couple of files that we want to turn into a shared library…

file0.c file1.c file2.c and a header file fheader.h

each of those functions serve their own purpose and their prototypes are included in the header file.

To create the shared library lets run this command

$ gcc -fPIC -Wall -Werror -Wextra -pedantic *.c -shared -o libfile.so

  • The -fPIC flag enables position independent code, which allows the code to be located at any virtual address at runtime.
  • -Wall enables all warnings our compiler could throw.
Connor Brereton