Program compilation
The process of compiling the program can be divided into 4 steps, such as editing, preprocessing, compiling and linking.
1. Editing
Editor: The process of importing a source file into a computer for modification and saving is called "editing."
2. Preprocessing
A preprocessor is a stand-alone program that is called by the compiler before the actual compilation begins. The preprocessor can delete comments , include files , and perform macro overrides .
The preprocessor changes the program text before the compiler according to instructions. The compiler sees the code text that the preprocessor has modified.
"#include", "#define", etc. are all compiled preprocessing. Preprocessing lines end without semicolons. In principle the preprocessing line can be written anywhere in the program, but it is recommended to write in the file header. The common preprocessing directives are as follows:
1) header file contains, #include <iostream>
2) macro, #define PI 3.14
3) conditional compilation, #ifndef _func_h_ #define _FUNC_H_ #endif
4) Other, such as #pragma
3. Compiling
Compilation is used to translate each compilation unit into a binary code file. In DOS and Windows environments, the suffix of the binary code file is . obj, and in the UNIX environment, the prefix is named . O .
The object that the compiler handles is actually a compilation unit consisting of a single. cpp file and a header file that is recursively contained. When a. cpp file is compiled, the preprocessor first recursively contains the header file, forming a single source file containing all the necessary information, which is a compilation unit. This compilation unit is translated as a target file (. o or. obj) with the same name as the. cpp file.
Each. cpp file corresponds to a compilation unit, and each compilation unit generates a binary code file. Therefore, each. cpp file corresponds to a binary code file.
4. Links
The purpose of the linker is to combine the compiled fragmented binary code files into binary executables (. exe) files. It has two meanings:
1) Parse the functions and variables in the compilation unit;
2) Establish a link to the library function
A link is a procedure that assigns an absolute address to a function of a variable in a program, making the binaries executable.
C + +-Program compilation