C Advanced Development Notes - Development Notes 19: Introduction to the Compilation Process

--The difficulty of things is far less than the fear of things! 

    In this chapter, let's talk about the compilation process. We usually use an off-the-shelf integrated development environment to write code, such as vs2010, etc. After writing, we can get the executable file we want by one-click compilation, then compile with this key. process, what happened? What is the process of compilation? To understand this process, let's first understand the compiler.

    The compiler can be roughly composed of the following parts:


From the figure, we can know that the compiler consists of a preprocessor, a compiler, an assembler, and a linker. For the compilation process, you can refer to the following figure:


    As can be seen from the figure, the steps of the code from the source program to the object file are more responsible. Let's analyze them one by one:

    Precompiled:

        - Process all comments, replace with spaces

        - Remove all #defines and expand all macro definitions

        - Handle conditional compilation directives #if, #ifdef, #elseif, #else, #endif, etc.

        - handle #include, expand included files

        - Keep #pragma directives that the compiler needs to use

        The precompiled instructions in gcc are: gcc -E file.c -o file.i

        The precompiled instruction in VC is: cl -E file.c > out.i

Here's an example:

//header.h

char* p = "Delphi";
int i = 0;
//main.c
#include "header.h"

// Begin to define macro

#define GREETING "Hello world!"
#define INC(x) x++

// End

intmain()
{   
    p = GREETING;
    
    INC(i);
    
    return 0;
}
Use the precompiler to compile and compile under vs2010, cl -E main.c > main.i, the content of the main.i file is as follows:
#line 1 "main.c"
#line 1 "f:\\study\\c\\header.h"

char* p = "Delphi";

int i = 0;
#line 2 "main.c"

intmain()
{   
    p = "Hello world!";
    
    i++;
    
    return 0;
}

From the content of main.i, we can know that the header file is expanded, the comment is removed, the #define is removed, and the macro is replaced in the corresponding position

Compile:

    - After the precompile is completed, the compiler will perform lexical analysis , syntax analysis and semantic analysis on the precompiled file

        - Lexical analysis: analyze whether keywords, identifiers, literals, etc. are legal

        - Syntax analysis: analyze whether the expression follows the grammar rules

        - Semantic analysis: further analyze whether the expression is legal on the basis of syntax analysis

    - After the analysis is completed, code optimization is performed to generate the corresponding assembly code file

    The compilation instructions in gcc are: gcc -S file.c -o file.s

compilation:

   - The assembler converts the compiled assembly code into instructions that the machine can execute

    -Every assembly statement corresponds to almost one machine instruction

Summarize:

    The compilation process is divided into four stages: preprocessing, compilation, assembly and linking

        - Preprocessing: process comments, macros and symbols already starting with #

        - Compile: perform lexical analysis, syntax analysis and semantic analysis

        -Assembly: An object file that translates assembly code into machine instructions


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324684821&siteId=291194637