Dynamic link and static link and link library

Dynamic link and static link and link library

Preface

This article merges and organizes several related blog posts, and the reprint addresses of some of the contents are as follows to explain static links and dynamic links in a simple way.

Dynamic link and static link

The biggest difference between static linking and dynamic linking is that the timing of linking is different. Static linking is before the executable program is formed, while dynamic linking is performed during program execution.
1. Static linking
1. Why do you need to statically link
multiple source files? There are multiple dependencies between multiple source files. One source file may call a function defined in another source file, but each source file is compiled independently. A *.c file will form a *.o file. For the dependency between the source files, the target files generated by these source files need to be linked to form an executable program. This process is static linking.
2. The principle of static linking
is a static library formed by linking many object files. The static library can be simply regarded as a collection of a set of object files, that is, a file formed after many object files are compressed and packaged, as shown below, use The -a parameter of the ar command view the composition of the static library:
Insert picture description here

链接器在链接静态链接库的时候是以目标文件为单位的。比如我们引用了静态库中的printf()函数,那么链接器就会把库中包含printf()函数的那个目标文件链接进来,如果很多函数都放在一个目标文件中,很可能很多没用的函数都被一起链接进了输出结果中。由于运行库有成百上千个函数,数量非常庞大,每个函数独立地放在一个目标文件中可以尽量减少空间的浪费,那些没有被用到的目标文件就不要链接到最终的输出文件中。

3. Advantages and disadvantages of
static linking Disadvantages of static linking:
First, waste of space, because each executable program must have a copy of all required object files, if multiple programs have dependencies on the same object file, such as If the printf() function is called in multiple programs, these multiple programs all contain printf.o, so there are multiple copies of the same object file in the memory;
second, it is difficult to update, whenever the code of the library function is Modification requires re-compiling and linking to form an executable program. But the advantage of static linking is that the executable program already has everything needed to execute the program, and it runs fast during execution.

Second, dynamic link
1. Why
dynamic link appears The reason why dynamic link appears is to solve the two problems mentioned in static link, on the one hand it is a waste of space, on the other hand it is difficult to update.

2. The principle of
dynamic link The basic idea of ​​dynamic link is to split the program into relatively independent parts according to the modules, and link them together to form a complete program when the program is running, instead of all program modules like static linking All are linked into a single executable file. The following briefly introduces the process of dynamic linking:

Suppose there are two programs program1.o and program2.o, both of which share the same library lib.o. Suppose that the program program1 is run first, and the system first loads program1.o. When the system finds that program1.o uses lib.o , That is, program1.o depends on lib.o, then the system loads lib.o, and if program1.o and lib.o also depend on other target files, they are all loaded into memory in turn. When program2 is running, program2.o is loaded in the same way, and then it is found that program2.o depends on lib.o, but at this time lib.o already exists in the memory. At this time, it will not be reloaded, but the memory is already The existing lib.o is mapped to the virtual address space of program2 to link (this linking process is similar to static linking) to form an executable program.

3. The advantages and disadvantages of dynamic linking
(1). Each program depends on the same library, but the library does not have multiple copies and copies in memory like static linking, but these multiple programs share the same copy when they are executed ;

(2). The update is also more convenient. You only need to replace the original target file during the update, and you don't need to relink all the programs again. When the program runs next time, the new version of the target file will be automatically loaded into the memory and linked, and the program has completed the upgrade target. But dynamic linking also has disadvantages. Because the link is postponed until the program is running, it needs to be linked every time the program is executed, so there will be a certain loss in performance.

According to estimates, the performance loss of dynamic link and static link is about 5% or less. Practice has proved that this performance loss is worth using for the space saving of the zone swapping program and the flexibility of program construction and upgrade.

4. How to relocate the dynamic link address?
Earlier we talked about the address relocation during static linking. Then we are now thinking about how to relocate the dynamic link address? Although dynamic linking delays the linking process until the program is running, when forming an executable file (note that forming an executable file and executing a program are two concepts), a dynamic link library is still needed. For example, when an executable program is formed, it is found that an external function is referenced. At this time, the dynamic link library is checked and the function name is found to be a dynamic link symbol. At this time, the executable program does not relocate this symbol, but instead Leave the process until loading.


Link library

1. Compile and link separately (Linking)

Most high-level languages ​​support separate compilation. Programmers can explicitly divide the program into independent modules or files, and then compile each independent part separately. After compilation, the linker "glues together" these independent fragments (called compilation units). (Think about the benefits of doing this?)
In C/C++, these independent compilation units include obj files (compiled from general source programs), lib files (statically linked function libraries), dll files (dynamically linked Function library) etc.

Static link method: complete all assembly work before the program is executed, and generate an executable object file (EXE file).
Dynamic link mode: After the program has been loaded into memory for execution, the link is completed, and generally only a copy of the compilation unit is kept in the memory.

Two, static link library and dynamic link library

Let me first explain the concept of Dynamic Linkable Library (DLL). You can simply regard DLL as a kind of warehouse, which provides you with variables, functions or classes that can be used directly.

Both static link library and dynamic link library are ways to share code. If you use static link library, whether you like it or not, the instructions in lib are directly included in the final generated EXE file. However, if a DLL is used, the DLL does not need to be included in the final EXE file. When the EXE file is executed, the DLL file independent of the EXE can be "dynamically" referenced and uninstalled.
The advantages of using dynamic link library: (1) Save more memory; (2) DLL file and EXE file are independent, as long as the output interface remains unchanged, changing the DLL file will not have any impact on the EXE file, thus greatly improving maintainability And scalability.

Three, understand the dynamic link library

Dynamic linking is relative to static linking. The so-called static linking refers to linking the function or procedure to be called into the executable file and becoming a part of the executable file. In other words, the code of the functions and procedures is in the exe file of the program, which contains all the codes needed at runtime. When multiple programs call the same function, there will be multiple copies of this function in memory, which wastes valuable memory resources. The function code called by dynamic linking is not copied to the executable file of the application, but only the description information of the called function (usually some relocation information) is added to it. Only when the application is loaded into the memory and starts to run, under the management of Windows, the link relationship is established between the application and the corresponding DLL. When the function in the called DLL is to be executed, according to the relocation information generated by the link, Windows will switch to execute the corresponding function code in the DLL. In general, if an application uses a dynamic link library, the Win32 system guarantees that there is only one copy of the DLL in the memory

Two link methods of dynamic link library:

(1) Load-time Dynamic Linking: The premise of this usage is that it is clear which functions in the DLL should be called before compilation, and only necessary link information is retained in the target file during compilation. It does not contain DLL function code; when the program is executed, the link information is used to load the DLL function code when calling the function and link it into the execution space of the calling program in the memory (all functions are loaded into the memory), the main purpose is Facilitate code sharing. (Dynamic loading program, in the loading stage, mainly for sharing code, sharing code memory)

(2) Run-time Dynamic Linking: This method means that you don’t know which DLL functions will be called before compiling. It is completely determined which function should be called and loaded during the running process. To the memory (only load the called function into the memory), and identify the memory address, other programs can also use the program, and use LoadLibrary and GetProcAddress to dynamically obtain the entry address of the DLL function. (There is only one copy of the dll in memory and it is in the running stage)

The above-mentioned difference mainly lies in the different stages, whether the compiler knows the dll function to be called by the process. Dynamic loading knows the called function at compile time, but must not know at runtime.

Guess you like

Origin blog.csdn.net/weixin_44524004/article/details/112620147