Static library vs dynamic library

David Jesus Orozco Garcia
5 min readMay 5, 2020

I’m a big fan of comparisons and metaphors, so I’ll explain this topic, with a lot of examples and in a simple way. So let’s start.

What are libraries?

Libraries are a set of functions, macros, structures, etc, that can’t do anything alone.

Wait, you don’t get it? Ok, let me explain with an example.

Imagine that a library is a body without a head but at the same time, is a powerful tool without energy.

  • It’s a body because you have a structure and lot of parts
  • Don’t have a head, because a library needs an entry point or the main function in a simplified way
  • Why do I say that’s a powerful tool without energy? Before you judge me, let me explain. Can you use a tool without energy? The answer is no, right? Because you need a source, a point to start, a guide, need more than a brain, you need again, an entry point.

Do you get it now?

Perfect!!!. Maybe you have another question, why do we use libraries? And guess what, I have the answer

You asked the wrong question, the real question is, why do we have different tools, programs, professionals?

The answer is quite simple. You can have different things with a unique purpose, an assigned task.

Yes, but I just wanted to give you a short example.

The example in the programming world is to not create a mess, reuse code, be more efficient, don’t reinvent the wheel and a lot of good practices like clean code and KISS.

Pros of this? Clean, efficient, custom structured, and scalable. Cons?

We have two types of libraries, static and dynamic

Static libraries

Static libraries (suffix .a) are linked with the head (program, entry point…) in compilation time. That means that we you generate your entire program, the library is going to be copied to the program.

As you can imagine, each program makes the copy according to their needs. Each copy between a program and a library is isolated from the rest.

It’s possible because are isolated

If you want more information about static libraries, check my previous post.

Dynamic libraries

Dynamic libraries (suffix .so) are linked with the head in runtime, so the program uses the library while it is running. So? When you compile your program, you’re just compiling your program, just that.

So when will the program meet the body? Well, in runtime, when the program is running, the head searches the library and takes what it needs.

An aka for dynamic is shared, which means that the library and the program is not isolated. For example, the following image it’s not possible.

Why? As the library is shared, that means that always is using the last version and changes, so all the programs that depends of this, should have the same implementation. In this case, the correct example is:

I don’t want to extend more the blog, so I’m going to do a resume.

  1. Create prototypes
  2. Define the prototypes
  3. Compile library
  4. Set the path
  5. Run

First, we need a header file. So let’s go.

This is how to compile a dynamic library but it is superficial if you want to know more about the flags that we used before, check this explainshell.

This step is simple but is really important. You have to set the location of the library, why? Remember that it is a shared library, so all the processes and programs can use the library, and the only way to localize it is via the environment.

Comparison between both

So, what are the main differences? What should I use? Which is better?. Let’s go-to answer and fill some voids at the same time.

As you can see, both have a purpose:

  • The app is for a company, static
  • The app is for the development kit, dynamic
  • The app is under development, both
  • The app is designed for microchips, both

Follow me on Twitter and GitHub

--

--