Dynamic Libraries

Brayan Florez
3 min readDec 16, 2019

--

What is a library?

A library is a collection of several object files (.o, .obj), that can be used as single entity in a linking phase of a program. it actually means that those files are generated one step before linking, in other words, before creating the executable file.

Why should we use libraries?

We should use them because they allow us to package a big group of functions (code) inside it and “combine it” to the executable file (.exe).

How to create them? (Linux only)

You can see how to create a static library in this article that I wrote.

We can create a dynamic library by following the next two steps:

  1. Create object code: To create the files .o that we are gonna use in our dynamic we need to execute the following command
gcc -c -fpic *.c

The flag -fpic is a characteristic required by dynamic libraries which generates position independent-code

2. Create library: To create the dynamic library you must run the following command

gcc -shared -o library_name.so object_file.o

The flag -shared produces a shared object which can then linked with other objects in order to create an executable

The flag -o places the output in the file, if it is not specified, the default puts the executable in a file called it a.out

By the following the previous steps your dynamic library must be now created with the extension .so

How to use them? (Linux only)

You can see how to use static libraries too in my previous blog.

If you want to link all functions of the library in the code(file) you need to compile it with the following command

gcc -Wall -pedantic -Werror -Wextra -L. main.c 
-l'name_of_your_library' -o

-L flag allows us to get the function we need in our .c file and that is the awesomeness of dynamic libraries. If you paid enough attention you should have checked our library name it’s followed by -l flag and it doesn’t have the extension .so

To use a dynamic library we know that it has to be shared during the linking with other programs. So, we need to add a path to the library to the environment variable by running the next command

export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

If you need to see the functions what the library has you must use nm command followed by your library name and you’ll see a list like the next one

What are the differences between static and dynamic libraries

The main difference between these libraries is that in static libraries object files are used only during the linking phase, they are not required during the “runtime” meaning that only the executable file is necessary to run the program. And in shared or dynamic libraries we need both the executable and the library in order to compile the program since it is not attached to it.

What are the advantages and drawbacks of each of them?

Static libraries

Advantage:

It’s faster since the library is preloaded inside the executable file.

Drawback:

Every time that you want to add a function you need to re-compile the whole library again.

Dynamic libraries

Advantage:

You don’t have to recompile your programs when you add new code to your library.

Drawback:

Load times are longer since the linking occurs at runtime.

That’s all.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response