Differences between static and dynamic libraries

Sara Hincapie Monsalve
4 min readDec 16, 2019
Image taken from

What do you expect to find when you go to the library? … A collection of books right? Therefore a library in C is a collection of functions, variables and routines you can call from your program, and work with them. In c we find two kind of libraries, static and dynamic libraries, the two of them are very useful and we are going to see why.

Libraries are used to save time, by not needing to create a function for those tasks that are more used and common every time you are creating a program, also is easy to find symbols (functions, variables and so on) in them.

Do you remember we talked about the gcc compiler and how it works? (if not click here) and we also talked about compiling an static library, (here is that also) then let’s take a look to the difference between static linking and Dynamic linking.

Image take from

The compiling process is basically the same as the static library and we will explain it further. In the picture above we can see the difference in the type of files that are created with the library is created, in the case of the static the result is a .a file and in the dynamic library is a .so file.

But what is the difference, just the type of file that is created and it’s size?

The main difference between this two, the static library has to be recompile every time you make a change to a function in it, but with the dynamic library you don’t have to do this, it is automatically upgraded. The dynamic library can help us save a lot of memory and the executable files are much smaller. Because the dynamic libraries are linked in two stages launching the program is slightly slower than running it with an static library.

How to create a Dynamic Library?

We need the GCC compiler to make our functions (.c files) object files (.o files) and we do so by using this command.

We use the prefix -c to make our .c files objects

The -wall flag is to include warnings. The -fPIC flag is a Compiler directive to output position independent code, a characteristic required by shared libraries (dynamic libraries)

After using this command we now have our object files, remember we said we link in two stages so now we compile again like this.

The -shared flag produce a shared object which can then be linked with other objects to form an executable, and the -o flag is Output of operation. In this case the name of the shared object to be output will be “libname.so”

Now we have to shared the dynamic library during the linking with other programs and to do it we have to add a path to the library environment variable ( LD_LIBRARY_PATH).

In case of our example, it is the current working directory, and we can use the . to add its path. Now the operating system is aware of where to look if some program will request a functionality from the library.

Last but not least when we need to run a program with the library we must compiled like this:

The -L flag specifies the path to the library, in our case it is current directory, and -l flag specifies the name of the library to use. Please note that we didn’t provide the lib prefix, as well as the .so extension: they were resolved by the compiler.

--

--