A brief description of how a .c file becomes an .exe file, including precompiled compilation and linking of source files

a word

        A sentence of "Hello World" is almost the beginning of many small partners' exposure to programming, but behind the output of a sentence of "Hello World" is the result of several times of processing by the compiler.

        The current integrated development environment (IDE) is very smart and simple. Just press the compile button and wait for completion to get the execution program. The precompilation, compilation , assembly and linking are not displayed (as shown in the figure below).

         If there are multiple source files, they are precompiled , compiled, assembled, and linked together to generate an executable program (as shown below)

Two specific functions of precompiled compilation and assembly link 

2.1 Precompile

        1: Complete so #define replacement and deletion

        2: Process all precompiled instructions, such as #if, #ifdef, #endef, etc.

        3: Process the "#include" precompiled directive, and insert the included file into the location of the precompiled directive. Note that this process is recursive, which means that included files may also include other files.
        4: Delete all comments "//" and "/* */".
        5: Add line number and file name identification, such as #2 "hello.c" 2, so that the compiler can generate line number information for debugging during compilation and can display the line number when compilation errors or warnings are generated during compilation.
        6: Keep all #pragma compiler directives, because the compiler needs to use them.
Summary: The precompiled .i file does not contain any macro definitions, because all macros have been expanded, and the included files

 2.2 compile

Function: Perform grammatical analysis, lexical analysis, semantic analysis and optimization on the          source code to generate corresponding assembly code files (the next step is assembly, so assembly code needs to be generated)

2.3 Assembly

        Function: Convert assembly code into instructions that the machine can execute

Expansion: The assembler converts the assembly code into machine-executable instructions, and each assembly statement almost corresponds to a machine instruction. Therefore, the assembly process of the assembler is relatively simple compared to the compiler. It has no complicated syntax, no semantics, and does not need to optimize instructions. It is just based on the comparison table between assembly instructions and machine instructions-translation is enough. The name "compilation" also comes from this.

2.4 link

        Function: allocation of address and space, merging of segment tables, merging and relocation of symbol tables

expand:

Relocation: The absolute storage address modified by the linker (such as variables, etc.)

Guess you like

Origin blog.csdn.net/ZhuGeBin26/article/details/128436849