Everything you need to understand dynamic libraries and static libraries

Victor Zuluaga
5 min readSep 7, 2020

--

Intro

This article has assigned by Holberton as part of my formation as an Engineer’s software, this will help me to develop skills technical writing. “it’s not enough to just be able to code”.

Environment

cases of example are written on the following system:

  • Ubuntu 14.04 LTS
  • GCC compiler
  • VM(Virtual Machine) and Vagrant

Prerequisites

In order to fully understand this article, you need to know:

  • Knowledge Basic handling Linux OS +14.04 LTS
  • Some shell
  • Knowledge basics in C programming language.
  • Knowledge basic Compiler’s steps (but, not mandatory).

Libraries

Every time we create files and functions in our day by day as programmers, we notice that some functions can be used more than once in the different files that we are creating, for example:
We can have several files that call the functions we’ve created before: add, sub, mul, div.
we’ve noticed that we’re writing over and over the same code in the different directories that we’re working on.
So, this is what libraries for? Yes, it’s right.
before further going, we define what are libraries in computer science?

A library is one or more functions already compiled ready to be used in any program. Libraries in C allow us to create a single file with all the functions within itself already compiled to be able to use it whenever we want. This brings enormous advantages as:

  • Let’s don’t have that to write the same code over and over or (to do copy-paste).
  • Let’s don’t have compiler again the functions.
  • The already compiled code is tested and reliable to use.

To do this, Linux allows us to create 2 types of libraries known as Static and Dynamic.
Static Library: This library creates a copy of the function that we are going to use from the library and links it with our final program.
What is its advantage?
-
Once our executable file is compiled. we can delete the library and it would continue working.
- Another computer or directory can execute it without the need for the library, and it will continue working.
Disadvantage?
The code takes up a lot more space and in huge programs, it can be slower to execute it.

Dynamic Library: It’s NOT copied into our program when we’ve compiled. When we have our file and we’re executing it, every time the code needs something from the library, it will look for it. If we delete the library, our program will give an error that it cannot be found.
Advantage?
- Our program only takes the space needed.
- It’s more useful for big projects.
- It can be allocated in the PATH environment.
Disadvantage?
- If we delete the library our executable may stop working.

What kind of library do I use?
For small programs and for simplicity, it is recommended to use a static library, the dynamics are intended for huge programs or for system libraries.

On Linux the static libraries are usually called libname.a and the dynamic libname.so, where name is the name of our library (name: whatever you want).

Creating our first library
In this case, we’ll focus on creating a Dynamic Library. however, if you want to know more about Static Library, Click over on.

We’ll create 2 files prototype.h in this will save all prototypes and in function.c we’ll create the functions add, sub, and main will call the functions.

To start we need to create the object files first with command:
gcc -fPIC -c *.c

gcc: It’s the compiler’s C.
-fPIC: generates position independent code (PIC) for shared libraries. Do you know more?, Visit: https://stackoverflow.com/questions/5311515/gcc-fpic-option
-c: This flag is ised by the compiler to generate object code.
*.c: Apply all file that ends with .c

As you can see, we’ve created the file .o this is object code that it will be into our library.

Now, Let’s create our own library. Let’s use the next command.
gcc -shared *.o -o libmylibrary.so

  • shared: say it to the compiler that we want to create a shared library or dynamic library.
    *.o: Apply all file that ends with .o
    -o: tell it to the compiler that we want a name specific for our library.
    libmylibrary.so: it’s the name assigned to our library.

Okay, so far we’ve created our library. Now, let’s use it.
To execute your library use the follow command.

gcc -pedantic -Werror -Wextra -Wall -L. main.c -lmylibrary -o mylib

Once the executable is compiled, we need one last step. You have to tell the program, while it is running, where the dynamic libraries are, since it will look for them every time a function is called. We have to define the environment variable LD_LIBRARY_PATH, in which we put all the directories where there are dynamic libraries of interest. if your execute your file, it displays on error. Let’s see.

don’t forget . and .. it refer current directory and the previous one.

Thank you for reading.

If you have questions or feedback don’t hesitate to ping me on Twitter @VictorZulRam94

Files

This repo on github you can find all files and source code.

#define DYNAMIC_LIBRARY_H

int add(int a, int b);

int sub(int a, int b);

#endif

--

--

Victor Zuluaga
Victor Zuluaga

No responses yet