the differences between static and dynamic libraries

Khalil Hassayoun
2 min readDec 16, 2020

so first of all i am going to be talking about

How to create a library :

Let’s use as an example the file “hello.c”. In both cases, we need to create object files first. This is done with the following commands:

Static:

$ gcc -c hello.c

Dynamic:

$ gcc -c -fPIC hello.c

Now we can create the ‘mylib’ library with our object file. Here’s how:

Static:

$ ar rc libmylib.a hello.o

Dynamic:

$ gcc -shared -o libmylib.so hello.o

how does libraries work ?

After the libraries have been developed, they can be connected to the gcc file containing the main feature, or to the entry point . Then the code will be connected to the executable program of the functions used in the program, and it will be ready to run!

why we use libraries ?

so libraries allow us to store function definitions, aka pieces of code that will be use in our executable program. It makes the tedious task of compiling a huge number of source files together avoidable, isn’t it great? Libraries are made of object files, with the extension ‘.o’, and can be made on Linux with the gcc compiler. Then, it’s up to us if we want to create a static or shared library.

differences between static and dynamic libraries ?

Static libraries, while reusable in multiple programs, are locked into a program at compile time. Dynamic, or shared libraries on the other hand, exist as separate files outside of the executable file.

--

--

Khalil Hassayoun
0 Followers

Holberton school student . Future software engineer