Static and Dynamic Libraries

Luz A. Perdomo
4 min readMay 5, 2020

--

Before we start we should ask ourselves a couple of questions

What are C libraries?
What are C libraries used for?
How to create dynamics libraries in C?

Well, in past publications I had already talked about static libraries, you can go there first. here.

The Usage of libraries.

As may see from my last article, we use libraries to supply our applications ith already existing functions and constants so we don’t need to redo things already made by the community. The usage of libraries can help us also to reduce time and effort, which are constant to consider in software development.

How do libraries work?

Basically a library is a compiled archive which contains function and constants that we can use as part of our logic in order to solve problems related to them in our apps. These files can be linked to our code by 2 methods, static linking, which merges them to our executable and dynamic linking, which will create a reference to the library in our compilation and will require it at execution time. These two methods separate the libraries in 2 categories: Static and Dynamic Libraries.

Differences between static and dynamic libraries

As you can see, the main difference between these two kinds of libraries is that for static libraries, the compilation of the app that uses them will merge them with the binary result.
Meanwhile, the dynamic libraries are compiled as references in our applications and at the moment the app is executed, the references are searched and then used by our binary app. This method lets us use libraries that may be common for other apps, and because of that meat already be installed in the computer that will execute them; which will also mean that the compiled app will weight less than one using the same libraries but linked statically.

How can we create dynamics libraries in C?

First of all, the .c extension files you want to add to the library must be placed in the current working directory. Clearly, there must also be the .h file where all the prototypes of the functions and the libraries used to execute the codes are found.
Then we proceed to compile the .c files by executing the following the command:

gcc -fPIC -c *.c

This command generates an .o object file for each source file .c

Then, we execute the following command to group all the needed .o files to our newly generated library:

gcc -shared -o libholberton.so *.o

This example will create a libholberton.so which will contain all the .o files that compiled (the *.o part).

With the library done we can import it to any other compilation as we need.so the next question would be:

How to use Dynamic Libraries?

Having that we already have a .so library (which Linux distributions have and use many of them), we can add it to the compilation of our apps by, the following:

First, compile the .c files in our project to.o ones, for example for our main.we would do:

gcc -c main.c

Then we would generate a final compilation referencing the route of our .so file by doing the following:

gcc -o len main.o libholberton.so

This will generate a compiled file that will not contain our .so library but a reference that will search our file in the directory at the moment the app is executed.

Is good to mention that these work also for libs stored in shared folders where usually other developers store libraries widely used by all the community like /user/lib and /lib.

So now you may be thinking:

What is are the advantages and disadvantages of using any of them?

To which can be summarized for Static libraries with these points:

  • Does not depend on any external file t be executed
  • Are not sensible to changes done to the libraries used to create the app
  • Make the application size bigger since the compiled file should include the static library

And for Dynamic libraries we have:

  • Make the app work with shared functions used by other apps and this gives certainty that these functions work as intended.
  • Make the app dependant that the library files exist where they were referenced when the app was compiled.
  • The libraries required by an app may differ in version form the ones used by the creator of the app which may cause malfunctions.
  • The apps are lightweight since the compiled file only contains its logic and the references to where to find the libraries needed to work.

In resume, Static and Dynamic libraries, are useful tools that can help us create robust apps using existing libraries, and any of the library types should be used depending on what we need for our app.

--

--