Differences between Static and Dynamic Libraries in CAndres MartinBlockedUnblockFollowFollowingMay 6source unsplash.
comInbuilt functions are grouped together and placed in a file called library, that’s the definition of a library in C.
Why using libraries in general?Libraries are useful when it comes to maximize your productivity, and allows the user to get access to a myriad of functions that have pre-defined output instead of writing your own code.
How do they work?All C libraries are declared in the header of a file, a library is saved as libame.
h the header file .
h should be guarded using these flags:#ifndef _H_NAME.
#endifThen this file should be included at the beginning of your C program like this #include "libname.
h” this allows us to get access to the functions inside the library.
How to create a Static Library (Linux only)?In order to create a Static Library use the command gcc -c *.
c to create object files, after that use this command:$ ar -rc libname.
oThis program creates a static library named libname.
a and includes copies of files ending in .
o which are object files.
If the library file already exists, it is replaced, if they are newer than those inside the library.
The c flag tells arto create the library if it doesn’t exist.
The r flag tells it to replace older object files in the library, with new ones.
Next, the library should be indexed using the command randlib as follows:randlib libname.
hrandlib is also used to re-generate the index.
How to use them (Linux only)?After the static library is created, eventually we will want to use it, to do this we need to add the library’s name to the list of object files given to the linker.
Using a special flag like -l, here is an example:$ gcc -llibname -L 0-main.
c -o exec_nameNote the usage of the -L flag, this flag tells the linker that libraries might be found in the current directory.
How to create a Shared Library (Linux only)?In order to create a Shared Library use the following command to create object files:$ gcc -fPIC -Wall -pedantic -Werror -Wextra -c *.
cAfter that use this command:$ gcc -shared -o liball.
oThen we have to add our working directory to the LD_LIBRARY_PATH environment variable:$ export LD_LIBRARY_PATH=.
:$LD_LIBRARY_PATHHow to use them (Linux only)?The usage of a dynamic library is similar to a Static Library:$ gcc -Wall -pedantic -Werror -Wextra -L.
c -lall -o exec_nameWhat are the differences between Static and Dynamic libraries?One of the differences is that static linked executables require more space compared to dynamically linked executables which is the disadvantage of static linking and an advantage of dynamic linking.
comStatic Libraries: A Static library or statically-linked library is a set of routines, external functions, and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, it produces an executable.
Another difference is, If there are several processes calling the same object module of a shared library simultaneously, Only one copy of the shared library resides in memory for dynamically linked executables, where static linking all things of library gets copied as part of static linking as a consequence multiple copies for a process resides in memory.
comShared Libraries: are .
These are linked dynamically simply including the address of the library.
Dynamic linking links the libraries at the run-time.
Thus, all the functions are in a special space in memory, every program can access them, without having multiple copies of them.
What are the advantages and drawbacks of each of them?Advantages of Static Libraries:They are usually faster than the shared libraries because a set of commonly used object files is put into a single library executable fileNever has a compatibility issue, since all code is in one executable module.
Drawbacks of Static Libraries:Static libraries are much bigger in size because external programs are built in the executable file.
The executable file needs to be recompiled if any changes were applied to an external file.
Takes longer to execute, because load time is longer.
Advantages of Shared Libraries:It is faster because the shared library code is already in memory.
There’s no need to recompile the executable.
Are much smaller, because there is only one copy of a dynamic library that is kept in memory.
Drawbacks of Shared Libraries:Programs rely on having a compatible library.
The dependent program will not work if the library is removed from the system.
Some helpful resources to deepen your knowledge:Building And Using Static And Shared “C” LibrariesLUPG Home] [ Tutorials] [ Related Material] [ Essays] [ Project Ideas] [ Send Comments] v1.
1 Table Of Contents…docencia.