Differences between Static & Dynamic libraries.
Why we use libraries?
As a programmer, you may find yourself using the same function or functions repeatedly. In this case, it is best to put this function or functions in a library to speed up the compilation of the program. C libraries store files in object code; during the linking phase of the compilation process files in object code are accessed and used. It is faster to link a function from a C library than to link object files from a separate memory sticks or discs.
Two types of libraries in C: Static and dynamic
There are two types of libraries in C static and dynamic. Dynamic libraries are shared libraries with specific functions launched during the execution of a program and contribute to “reduced memory consumption”(techopedia.com). Dynamic libraries are linked in two stages. Static libraries produce object files and standalone executable files (wikipedia.org). These libraries can be linked to a program without recompiling the code. Static linking or the linking of static libraries creates larger files because of the creation of the standalone (executable file) files.
The implication of this is that memory is conserved while using dynamic libraries since each application or program can access the dynamic library without needing an individual copy, as would be the case, if we were using static libraries. Although dynamic libraries afford the ability to alter source code without recompiling the entire program, static libraries’ execution speed at run-time is faster because the object code for the functions within the library are already in the executable file. As a result, multiple calls to functions are handled more efficiently than when using dynamic libraries.
Do you want to create a new dynamic library?
Just simply type the following command on your prompt:
gcc *.c -c -fPIC and hit return. This command essentially generates one object file <.0> for each source file <.c> The <
-fPIC> flag ensures that the code is position-independent. This means it wouldn’t matter where the computer loads the code into memory. Some operating systems and processors need to build libraries from position-independent code so that they can decide at runtime where they want to load it into memory. The <
-c> options just ensures that each <
.o> file isn’t linked yet.
Then you should type the following command:
gcc *.o -shared -o liball.so (instead of all you can change it and put the name you want to) and hit return. The wildcard
* tells the compiler to compile all the
.o files into a dynamic library which is specified by the
-shared flag. The naming convention for dynamic libraries is such that each shared library name must start with
lib and end with
Last but not least we need to export the path for libraries, so the program knows where to look for them by executing the following command:
If you want to know a little bit more about Static libraries i left you a link.