C/C++: Program Environment and Preprocessing (Part 1)

Table of contents

Program compilation and linking process

1. The preprocessing stage in the compilation process

2. Formal compilation stage in the compilation process 

3. The assembly phase of the compilation process

4. Linking process


Program compilation and linking process

The source code file of a program can only be converted into executable machine instructions (binary instructions) through a complex compilation and linking process.

An overview of the compilation and linking process:

  1. Compilation process: Each source code file that makes up a program is converted into an object file ( .o file under linux ) through the compilation process
    (note that the compilation process of each source file is performed independently )
  2. Linking process: each object file (.o file) is linked together by a linker (linker) to form a single and complete executable program (.exe file)

Compile and link process diagram:

1. The preprocessing stage in the compilation process

The # modified statement in the source code represents a preprocessing instruction;

The preprocessing phase of the compilation process (some text operations are performed in the preprocessing phase) mainly completes:

  • Everything in the header file pointed to by #include will be "copy-pasted" into the source file where the #include statement is located
  • Replacement of symbols defined by #define
  • Deletion of comments

Note: no syntax checking is done at this stage

After the preprocessing of a source code file is completed, a corresponding .i source code file will be generated

In the gcc compilation environment of win10, use vscode to preprocess the source code file , and enter in the terminal:

gcc -E ./文件名 -o 输出的文件名(任意取)

After the compiler executes the instruction, it will get the .i file corresponding to the preprocessed source file :

2. Formal compilation stage in the compilation process 

The main things to be done in the formal compilation phase of the compilation process are:

  • Perform syntax check and various analysis on the source code in the file (if the syntax is wrong, a compilation error will be reported ) (the compiler will not check whether each identifier is defined at this stage )
  • Translate the source code in the .i file into assembly code and generate the .s file
  • Summarize symbols

The symbol summary will summarize all the functions and global variable names in the .i file after modification , and prepare for the generation of the symbol table. ( The symbol table plays an important role in the linking process )

In the gcc compilation environment of win10, use vscode to formally compile the .i file , and enter in the terminal:

gcc -S ./test.i

Generate .s file (assembly code)

3. The assembly phase of the compilation process

The assembly phase of the compilation process mainly completes:

  • Compile the .s file storing assembly instructions into a .o file (binary file)
  • form symbol table

The modified names of all functions in the .s file (and the addresses of the corresponding function bodies) , and the modified names of all global variables (and the addresses of the instructions defining them) are stored in the symbol table ;

  • Note that if the current source file does not define a called function (or global variable ), then the address corresponding to the function (or global variable) in the symbol table will be an invalid address (wait until the link stage to complete the addressing)

for example:

4. Linking process

The main things that the linking process accomplishes:

  • Merge of each .o file
  • Combination of symbol tables of each .o file

 for example:

  • After the linking process is completed, if the invalid address in the symbol table still cannot be determined , the compiler will report a linking error , and whether each identifier is defined will be checked at the linking stage .
  • In C++ , the decoration rules for function names in the C language symbol table are modified , providing support for the underlying mechanism for function overloading . See: Function overloading http://t.csdn.cn/tYd4h

An overview of the compilation and linking process:

Guess you like

Origin blog.csdn.net/weixin_73470348/article/details/128943864