Static vs. Dynamic(Shared) Libraries

Amilcar Armmand
2 min readDec 15, 2020

In the C programming language, programs are designed to be efficient and easy to use , consisting of many small functions rather than a few larger functions. Functions can be located in one or more source files and those source files can be compiled separately and loaded together with other functions compiled into libraries. The functions in the libraries can be linked statically, or dynamically as shared objects. Using these libraries helps reduce the size of executable files and allows for components to be reused and shared with other applications, saving space. Since C is a complied language, it uses a complier to create the executable files. The compiler consists of the preprocessor, the compiler, the assembler and the linker.

Parts of the compiler.

Static libraries are collections of object files with the `.a` suffix created with the `ar` archiver program. Static libraries allow users to link to programs without having to recompile its code saving compilation time.

In order to create a shared object library (.so) or dynamically linked library (.dll) or in C from a .c file, we use the following gcc compiler with options to generate an object file that is position independent code from our source .c file creating an `.so` file:

`gcc -fPIC sample_program.c -o library.so`

What are the differences between static and dynamic link libraries and what are some of the benefits or drawbacks? Shared Libraries are libraries that are loaded by programs when they start (at runtime). When a shared library is used it allows all program run after it to use the new shared library. This allows us update libraries and keep our executable file smaller because it’s not packaged with all of it’s dependencies. Static libraries make programs less portable and more difficult to update because the entire program has to be recompiled, but this is not a concern on an enclosed system that does not need to be updated or upgraded.

--

--

Amilcar Armmand
0 Followers

Software Engineering Student at Holberton School