Dynamic and static link link

As a C / C ++ programmers to compile and link process to be clear in the chest. First outline the compiler is divided into three steps, first of all the source files pretreatment, this process is mainly to deal with some of the # define command or statement (such as macros, # include, pre-compiler directives #ifdef, etc.), generate *. file i; then compiled, this process is mainly lexical analysis, syntax analysis and semantic analysis to generate compilation files * .s; finally assembled, this process is relatively simple, it is to translate the corresponding assembly instructions into machine instructions, generates binary object files relocatable. These are the compilation process, the following describes two main ways Links - static linking and dynamic linking.

        Statically linked and dynamically linked biggest difference between the two is that the timing is not the same link, the link is still before the executable form, and is dynamically linked during program execution, the following detailed description to link these two ways.

A static link
1. Why should we be statically linked
        in our actual development, impossible to put all code in a source file, so there will be multiple source files, and between multiple source files are not independent, but It exists various dependencies such as a source file may be another call functions defined in the source file, but each of the source files are compiled independently, i.e. each forming a file * .c * .o file order meet the target file dependencies said earlier, you need to these source files generated by links to form a program that can be executed. This process is linked statically linked

2. Principles of statically linked
         by many object files are linked to form a static library, and vice versa may be static library is a collection of simply as a group of object files, i.e., a file object file after many compression packing is formed, as FIG. using the ar command -a parameter view static library consisting of:

 

        Here * .o object files in my previous blog, "write the source code for the whole process from running in memory Resolution" has said very clearly, I do not know can see it.

        In the following diagram to briefly explain the procedure from statically linked to the executable file, according to the use of header files and source files included in the library functions, such as printf stdio.h defined () function in libc .a find the target file printf.o (here would not consider printf (dependency function)), then the target file and the file we hello.o link form our executable file.

 

        There is a small problem, that is, from the above figure we can see inside a static runtime object file contains only one function, such as libc.a inside printf.o only printf () function, strlen.o there is only strlen ( )function.

        We know that the link in the link static link library when the target is a file units. For example, we refer to a static library printf () function, the linker will be included in the library printf () function of that object file links come in, if many functions in one object file, it may be a lot of useless functions are linked together into the output. Since there are hundreds of runtime function, the number of very large, each function independently on a target file can minimize wasted space, and those of the target file is not used, do not link to the final output file .


3. The advantages and disadvantages of statically linked
        shortcomings statically linked Obviously, one is a waste of space, because every executable program must have a copy of all the required target files, so if more than one program on the same target file has dependence, such as multiple programs are called printf () function, which all contain multiple programs printf.o, so there are multiple copies of the same object file in memory; the other is updated more difficult, because each when the code library function changes, and this time will need to be recompiled linked to form an executable program. But the advantage is statically linked, executable program has had anything to perform all procedures required to run fast at the time of execution.

problem:

Second, the dynamic link
1. Why is there a dynamic link
        reasons for the emergence of a dynamic link is to solve two problems mentioned in the statically linked, on the one hand is a waste of space, on the other hand is difficult to update. Here's how to solve these two problems.

2. The principle of dynamic linking
        the basic idea of the program is dynamically linked in accordance with section split into relatively independent modules, the program runs in when they are linked together to form a complete program, rather than statically linked, like all program modules They are linked into a single executable file. The following outlines the process of dynamic linking:

        Suppose there are two programs and program1.o program2.o, both share the same library lib.o, assuming first run program program1, first load program1.o system, when used in the lib.o system program1.o found that depends on lib.o program1.o, the system then loads lib.o, if program1.o and lib.o also depend on other target file, followed by all loaded into memory. When program2 run the same load program2.o, then found program2.o dependent on lib.o, but this time lib.o already exists in memory, this time will no longer be reloaded, but the memory has been the existence of lib.o mapped to the virtual address space program2 in order to link (the link process and statically linked similar) form an executable program.

3. The advantages and disadvantages of dynamic link
        dynamic link obvious advantage is that even if every program needs to rely on the same library, but the library does not exist as points on a copy in memory like static links, but this multiple programs in execution when you share the same copy; another advantage is that the update is also more convenient, only need to replace the original target file is updated, all without the need to re-link the program again. When run under the program, the new version of the target file will be automatically loaded into memory and link up program to complete the upgrade of the goal. But the dynamic link is flawed, because the link postponed until the program is running, each execution procedures need to be linked, so there will be some loss of performance.

        It is estimated that the dynamic and static link link performance compared to a loss of about 5% or less. Practice has proved that this is a performance penalty area procedures used to exchange savings in space and flexibility in the program to build and upgrade is worth it.

4. Dynamic link address is how to re-locate it?
        Earlier we talked about static linking relocation of an address, that we are now thinking about the dynamic link address is how to re-locate it? Although dynamic linking the linking process be postponed until the program is running, but in the formation of an executable file (note the formation of the executable file and execute the program are two concepts), or the need to use dynamic link library. For example, when we form an executable program, found references an external function, then checks the dynamic link library, found that the function name is a dynamic link symbol, this time not executable symbol of the relocation, and the this process is loaded till then.
----------------
Disclaimer: This article is the original article CSDN bloggers "kang___xi", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement. .
Original link: https: //blog.csdn.net/kang___xi/article/details/80210717

Guess you like

Origin www.cnblogs.com/liangyc/p/11628163.html