The compilation process consists of 4 processes
1) Compile preprocessing
The work done by the precompiled program can be said to be a "replace" work on the source program. Through this process, an output file with no macro definition, no conditional compilation instructions, and no special symbols is generated.
Macro-defined commands, such as # define
conditional compilation directives; for example, #ifdef
header file contains directives; # include
special symbols; for example __line__,__file__
pretreatment modules; For example #pragma
2) Compile and optimize the stage
Through lexical analysis and parsing, after confirming that all instructions conform to grammatical rules, they are translated into equivalent intermediate or assembler code.
In C + +, the following functions are generated by the compiler if you do not define them but are required: default constructors, copy constructors, destructors, assignment operators, and a pair of accessor operators.
3) Assembly Process
The process of translating assembly language code into target machine instructions. For each C language source process that is processed by the translation system, it will eventually get the corresponding target file through this processing. A machine language code that is stored in the target file, which is the target equivalent to the source program.
4) Linking programs
The relevant target files are connected to each other, and the symbols referenced in one file are connected to the definition of the symbol in another file, making all of these target files a unified whole that allows the operating system to be loaded into execution.
C + + uses the linker to ensure that the function used is defined only once, and that the static objects is defined only once, and that the function we have declared but not defined is detected.
Links have static links and dynamic links.
Static linking: In this way, the code of the function is copied from its location in the static link library to the final executable program. The code is then loaded into the virtual address space of the process when it is executed. A static link library is actually a collection of target files in which each file contains code for one or a set of related functions in the library.
Dynamic linking: In this way, the code of the function is placed in a target file called a dynamic link library or a shared object. What the linker does at this point is to record the name of the shared object and a small amount of other registration information in the final executable program. When the executable is executed, the entire contents of the dynamic-link library are mapped to the virtual address space of the corresponding process at run time. The dynamic linker will find the appropriate function code based on the information recorded in the executable program.
Note: In the header file we usually only place declarations rather than definitions, because header files may be released into multiple source files, each source file is compiled separately, so the link will find that there are multiple definitions in the global space.
C + + compilation Steps