The differences between Static and Dynamic Libraries

Why using libraries and what are they?

As functions, libraries are blocks of code that may be reused to save time and space in a program. The most significant advantage to use libraries is that instead of writing all your code by yourself, you can also get external ones. There is two types of libraries : the static and dynamic ones.

Why using static libraries?

The most significant advantage while using Static Libraries is that the application can be certain that all its libraries are present and that they are in the correct version. This avoids dependency problems. Static linking can also allow the application to be contained in a single executable file, simplifying distribution and installation. But the size of the executable is greater than in dynamic linking, as the library code is stored within the executable rather than in separate files.

Why using dynamic libraries so?

Using a dynamic library is good in the way that multiple running applications can use the same library without the need for each to have it’s own copy. Because dynamic libraries live outside of the executable file, the program need only to make one copy of the library’s files at compile-time. But it's not safe in every cases. If a dynamic library for example becomes corrupt or deleted, the executable file may no longer work properly.

So using one or another depends on the use you have of your program. If it's a program that needs frequent updates, you may think in a dynamic library which will be much lightweight than giving back the entire library each time. That's an example of use and you can think in many more cases where static libraries will be better!

Create Libraries in C

First of all, you need to have all your .c files you want to add. It’s easier to store them in one directory, like this, it will be easier to link them together for the next steps.

The Dynamic Libraries:

  1. gcc *.c -c -fPIC
    The code is not stored in text but in object files, that's why we use the option -c . The flag -fPIC (Position Independent Code) ensures that the generated machine code is not dependent on being located at a specific address in order to work.
  2. gcc *.o -shared -o liball.so
    A dynamic library is also called a shared library. That's why we use this flag (-shared). We tell gcc to link all .o files into a dynamic library called (-o) liball.so. The .so extension is used for dynamic libraries. Watch out! The name is really important, and it should always start with "lib" because the compiler will look after that to find them.
  3. export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH
    Finally, because a program needs to know where to look for library files, we must add that location to the environment variable LD_LIBRARY_PATH.

The Static Libraries:

I wrote a blogpost about creating static libraries, but here's the steps without too much explanation because there are almost the same as for dynamic.

  1. gcc -c *.c
    The code is not stored in text but in object files, that’s why we use the option -c to convert our .c files into object code.
  2. ar rc liball.a *.o
    A static library is an archive of object files. That’s why we use ar rc. Watch out again! The name is really important here too, and it should always start with "lib" because the compiler will look after that to find them. But for static libraries, the extension is .a
  3. ranlib libmylib.a
    Finally, we order or archive with ranlib. This index is later used by the compiler to speed up symbol-lookup inside the library, and to make sure that the order of the symbols in the library won’t matter during compilation.

How to use them now?

Once our libraries are made, following up the upper steps, you can use them by invoking them as part of the compilation and linking process when creating a program executable. Here is how:

$ gcc -L. -lall main.c
  • -L : path to the library
  • -l<library name>, without the lib prefix and the extension

It’s important to leave the lib and .so or .a out of the flag because the compiler already identifies library files that way.
This will work for either static or dynamic libraries!

And that’s it!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store