The relationship between C++ static library and dynamic library
What is a library?
Libraries are written existing, mature, and reusable codes. In reality, every program relies on many basic underlying libraries. It is impossible for everyone to start their code from scratch, so the existence of libraries is of extraordinary significance.
The so-called static and dynamic refers to links. To review, the steps to compile a program into an executable program:
Static library:
The reason why it becomes a [static library] is because during the linking phase, the object file .o generated by the assembly will be referenced to The libraries are linked together and packaged into the executable file. Therefore, the corresponding linking method is called static linking.
Just imagine that the static library and the target file generated by assembly are linked together into an executable file, then the static library must have a similar format to the .o file. In fact, a static library can be simply regarded as a collection of target files (.o/.obj files), that is, a file formed by compressing and packaging many target files. Summary of the characteristics of static libraries:
1. The linking of function libraries by static libraries is completed during compilation.
2. The program has nothing to do with the function library when it is running, making it easy to transplant.
3. It wastes space and resources because all related target files and involved function libraries are linked into one executable file.
Through the above introduction, I found that the static library is easy to use and understand, and it also achieves the purpose of code reuse. So why do you need a dynamic library?
Why do you need a dynamic library?
Why a dynamic library is needed is actually due to the characteristics of a static library.
1. Waste of space is a problem of static libraries.
2. Another problem is that static libraries can cause trouble to the update, deployment and release pages of the program. If the static library liba.lib is updated, the applications that use it need to be recompiled and released to users (for players, it may be a small change, but it will cause the entire program to be re-downloaded and fully updated).
Dynamic libraries are not linked to the target code when the program is compiled, but are loaded when the program is run. If different applications call the same library, they only need to have one instance of the shared library in the memory, avoiding the problem of wasted space. The dynamic library is loaded when the program is running, which also solves the problem that the static library will cause trouble to the update, deployment and release page of the program. Users only need to update the dynamic library, incremental update.
Summary of dynamic library features:
1. The dynamic library postpones the loading of links to some library functions until the program is running.
2. Resource sharing between processes can be achieved. (So dynamic libraries are also called shared libraries)
3. Make it easy to upgrade some programs.
4. The link loading can even be completely controlled by the programmer in the program code (display call).