The differences between static and dynamic libraries

functions in programming is a collection of statements that together perform a task and are easily reusable throughout a program which make the code more clean, short and easier to understand.on the other hand libraries are pre-compiled pieces of code that contain functions, classes, data structures and more and they could be reused in a program and even different programs at the same time.

static libraries are libraries that are usable by different programs but they are integrated with the program at compile time so after compilation they will only live inside a particular program and won’t be used by other programs and changing them would require recompilation for the program, However dynamic libraries exist separated from the executable program which means that they could be used with different programs at the same program.

static library :

pros: the final executable file is complete with all the needed library functions therefor it never has any compatibility issues.it executes faster.

cons: since the final executable file (a.out) created by the linker will include all needed library functions and data the size of the file is usually larger .changing anything in the library would require recompiling the program.

dynamic libraries :

pros: the size is usually smaller, because there is only one copy of dynamic library that is kept in memory. can run several programs which share the same library functions at the same time more efficiently, changing anything in the library won’t require recompiling the source file.

cons: programs are dependent on having a compatible library, which will not work if the library gets removed from the system.

1- using the compiler we should return one object file (.o) for each source file (.c) : this is done by using the -c flag and since the location of the library will vary from one program to another we can store data at fixed addresses : this is done by using the -fpic flag .if we want to add all source files in the current directory to a dynamic library we should run something like this:

gcc *.c -c -fpic

2- the object files are now ready to be compiled into a dynamic library. this is done by using the -shared flag. and using the -o flag you can define you library name (make sure you use the .so). adding all the object files in the current directory to a dynamic library should look something like this:

gcc *o -shared -o liball.so

3- now our dynamic library is ready to use but since a program needs to know where to look for library files we must add that location to the environmental variable LD_LIBRARY_PATH.


Static Library:

1- create the object files :

gcc *.c -c

2- archive the library:

ar rcs liball.a *.o

3- if you are using a header file for the prototypes in the library make sure to include it using:

#include <headerfile.h>

now finally when compiling the program we should tell the compiler to actually us the libraries and where are they. the compilation command should look something like this:

gcc -L. -lall -o program source_file.c