Strongly suggest you to get your own portfolio site and show us :)
In C++, there are two types of libraries based on distribution namely shared library (also known as Dynamic Library) and Archive Library (also known as Static Library). We have explored the differences between the two types in depth.
In short, the definition of the two library types are:
- Shared library (or Dynamic Library): is a library code that can be reused by multiple programs and exists as an external dependency to a program.
- Archive library (or Static Library): is a library code that is merged into the executable of the client code and thus, removing external dependencies.
The key differences between Shared (Dynamic) and Archive library (Static) in C++ are:
- archive library has extension .a while shared library has .so
- To create archive library, we need ar utility along with G++ compiler. For shared library, only compiler is enough.
- In case of archive library, we get one executable (as client code and library code is merged) and in case of shared library, we have multiple executables (seperate client and library executable).
- To load shared library, we need to set LD_LIBRARY_PATH and compile client code with the executable path. For archive library, we do not need any step.
Shared and archive library differs in the following aspects:
- File extension
- Compiling the library
- Linking with client code
- Dependency and file size
- Shared library has the extension ".so" like library.so. SO signifies shared object.
- Archive Library has the extension ".a" like library.a. A signifies archive.
An archive/ static library is created using the following commands:
// Convert library code to Object file g++ -c -o library.o library.c // Create archive file/ static library ar rcs library.a library.o
It requires ar utility.
On the other hand, to create a shared object file, we need to follow the following commands:
gcc -c -o library.o library.c gcc -shared -o libfoo.so library.o
Linking with client code
To link a C code with an archive library, we just need to pass it along with the client code during compilation:
g++ -std=c++14 code.cpp library.a
This will merge the code.cpp and library.a into the same executable.
For shared object library, we need to follow the following steps:
# Create the executable by linking shared library gcc -L<path to .SO file> -Wall -o code main.c -l<library name> # Make shared library available at runtime export LD_LIBRARY_PATH=<path to .SO file>:$LD_LIBRARY_PATH # Run executable ./a.out
Dependency and file size
If the size of client code is 10MB and the size of library code is 250MB, then:
In case of static/ archive library, we will have one executable of size 260MB. You need to share just one executable and anyone can run it.
In case of shared library, we will have two executables. One executable for client code and other executable for library code of size 250MB. We need to share all the executables to make sure the code is executable.
With this, you must have the complete idea of archive and shared library. As a rule of thumb, you shall use archive library when you want to get one executable (maybe to share it) and shared library to save memory on system.