Static and Dynamic libraries in C

Manuel Puerta Villa
5 min readMay 12, 2020

I’m Manuel and today I will reinforce my knowledge with you. Following the Feynman technique I will try to teach you why to use C libraries (static and dynamic), how do they work, how to create them, and how to use them. If in the end you learned something please let me know!

As I told in my blog post about Static Libraries Libraries are for those ones who want the thrill of efficiency. Doing simpler, smarter work.

A library allows you to save all the auxiliary or secondary functions of your code as other resources you could need. This way you can work in your main thread without worries and you know that your precompiled library will be there for you. But it’s more than that, it’s reusable, your library can be with use with other files, so once done you don’t have to worry again for copying all those auxiliary functions, just bring your trusty library with you.

In the C compilation process the stages are Prepossessing, Compile, Assembler and Linking.

The important part for libraries is the last step, Linking. Here the compile object files are linked to the library in two different ways:

Static Library

The library its add at the executable file. This way the executable is portable, but heavier. A big down side it’s that if you made any change to any file in the in the library and want to update the executable, you have to recompile your library and then recompile your executable.

Dynamic o Shared Library

The library its not added at the executable file but the address of the library. So the this way the executable its dependent of the existence of the library in the system but is litter. The big up side it’s that if you made any change in the library, you don’t have to change the executable.

There is also a interesting new risk in Dynamic or Share Library linking and it’s in the fact that if the library (In your system) can be replace or for an erroneous or malicious code. An example of this is changing the variable LD_PRELOAD witch creates a dangerous treat.

Some comparatives

Comparation from GeeksForGeeks Web page

How to create them

Static Library

  1. Create Object files of our functions (gcc -c +functions)

2. The tool to create static libraries is “ar”.

We use the -c flag create the library if doesn’t exist and the -r to add the new objects files to the library or replace the old ones in case they already exist.

Creation of the library from the Object files

3. Indexing with ranlib

After an archive is created, or modified, there is a need to index it. 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

We will use the flags -L and -l.

With -L we will say in witch directory look for our library. -L + dot (-L .) will indicate the current directory and -l will look for the library name “library” witch is kind of funny in my opinion.

With the -l flag
With out the -l flag

Dynamic o Shared Library

  1. Create Object files of our functions (gcc -c +functions + fPIC flag)

2. Create the static library with the gcc -shared flag

In this example the new library will be “liball.so

ldd

Check if the system locates the library properly and prints the shared libraries that the program or file required in order to operate

LD_LIBRARY_PATH

It’s an environment variable, and contains a set of directories where libraries should be searched for first, before the standard set of directories. I this case we are setting the env. variable to be after searched after the current working directory. So now the Library can be found by the executable file.

ldconfig

Creates update and removes the necessary links and cache to the most recent shared libraries found in the directories specifies in the command line.

If you want to see what functions does your library have, you can use:

  1. ar -t →For static libraries

The flag -t is used to display a table listing the contents

2. nm

That’s all I know. Hopefully you learn something!

Have a good day!

--

--