Dynamic Libraries vs Static Libraries
Why using libraries in general?
Libraries provide the user the benefit to use a variety of functiones coded in different files in programs. Each function can be found in different files, in a main.h file we just need to store all the prototypes of each function and include the main.h file header in the .c files that contain the function's code. This avoids having to write a function's code in each .c file that wishes to call a function. And continue with more steps detailed below.
How do Dynamic Libraries work?
Dynamic libraries are shared libraries which launches special functionalities only during program execution, which minimizes overall program size and facilitates improved application performance for reduced memory consumption.
How to create them in Linux?
As we are programming in c language, ".c" files that contain functiones are compiled into object files (-o).
So we compile all ower .c files by typing in the terminal:
gcc *.c -c -fpic
Since multiple programs can all use one instance of a dynamic library, the library can’t store data at fixed addresses. This is because the location of the library in memory will vary between programs. This is done by using the compiler flag -fpic. Since we need to apply this step after the compile process has generated the object code, the compiler must be told to halt and return one object file (.o) for each source file. This is done by using the -c flag.
Next step:
Type:
gcc *o -shared -o libname.so
The object files are now ready to be compiled into a dynamic library. This is done by compiling all of the .o files using by using the -shared flag. Later when compiling program files, the compiler identifies a library by looking for files beginning with ‘lib’ and ending with a library extension (.so)
Last step:
Type:
export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH
Because a program needs to know where to look for library files, we must add that location to the environmental variable LD_LIBRARY_PATH.
How to use them?
Now that your Dynamic Library is done let's use it.
Recommended by LinkedIn
To list your libraries use the next commands:
To name the functions in your library:
nm -D libname.so
What are the differences between Static and Dynamic Libraries?
The main difference between these two is that a static library must be linked into the final executable; it becomes part of the executable and follows it wherever it goes. A dynamic library is loaded every time the executable is executed and remains separate from the executable as a DLL file. This difference is the one that you will take into consideration to choose one of both options.
What are the advantages and drawbacks of each of them?
The downside of using a static library is that it’s code is locked into the final executable file and cannot be modified without a re-compile. In contrast, a dynamic library can be modified without a need to re-compile.
Because dynamic libraries live outside of the executable file, the program need only make one copy of the library’s files at compile-time. Whereas using a static library means every file in your program must have it’s own copy of the library’s files at compile-time.
The downside of using a dynamic library is that a program is much more susceptible to breaking. If a dynamic library for example becomes corrupt, the executable file may no longer work. A static library, however, is untouchable because it lives inside the executable file.
The upside of using a dynamic library is that multiple running applications can use the same library without the need for each to have it’s own copy.
Another benefit of using static libraries is execution speed at run-time. Because the it’s object code (binary) is already included in the executable file, multiple calls to functions can be handled much more quickly than a dynamic library’s code, which needs to be called from files outside of the executable.
To read more in detail about Static Libraries and how to create them check this blog:
Hope you find it usefull :)