Differences between static and dynamic libraries

Felipe Londoño
3 min readDec 17, 2019

To understand the difference between static and dynamic libraries we need to define first both of them.

Static Libraries

A static library is a way to save multiple functions inside one file, this is useful since you can reuse code in different projects, the only issue is that as the name says this kind of libraries are static so the only way to add more functions is recompiling the whole library.

Dynamic Libraries

On the other side, we have the dynamic libraries, the perks of using this kind of libraries are that the system dynamically links the object files to create the library.

The main difference between these two is the way how these libraries link the object files, in other words, the functions in a static library live inside the executable file and in the dynamic are linked in the moment of the compilation.

How to create Dynamic Libraries (Linux only)

$gcc -c -fpic *c

In this case, we are using de GDB compiler with some flags, the -fpic is used to generate position-independent code suitable for use in a shared library. The -c compile or assemble the source files, but do not link. The linking stage is not done. The output is a .o file (object file).

gcc -shared -o libctest.so.1.0 *.o

To continue the process we use the flag -shared, these create a shared object which can then be linked with other objects to form an executable, and then we select the output name of the library.

export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH

Finally, we’ll need to export the path for libraries so that programs know where to look for them.

How to create Static Libraries (Linux only)

gcc -c *.c

That is going to compile into objects (.o) all the functions that we have ending in “.c”.
Now to create the library we need to use the tool “ar” (archiver) with the flags “-rc” this is going to create and replace the older files when needed.

ar -rc nameoflib.a *.o

That is going to add all the object files to the library that we are creating.
To complete the process we just need to use the command “ranlib” to index our library.

ranlib nameoflib.a

To use the static library we need to add the library name to the compilation and linking process of our main file, as we are using GCC we need to add two flags, the -L that is used tell the linker that the library can be found in the current directory and -l in front of our library name to link the library to our file.

How to use them (Linux only)

To use a static library we need to compile using the following flags

gcc -L file.c -ldynamiclib -o output_name

On the previous command, we are placing the flag -L that tells the compiler to look in the current directory for the library file, then thefile.cthat is just the file that were are compiling and the name of the library combined with the flag -l, the last part is just the -o to write the name of the output file.

To use a static library we need to compile using the following flags

gcc -Wall -L/opt/lib prog.c -lctest -o prog

--

--