The following describes how to compile C ++ in the C ++ compiler. The C ++ compiler is a compilation environment highly compatible with standardized C ++. This is very important for compiling and porting code, which is described in detail below.
For example, ifdef/ifndef is to pick out some qualified code from a file and hand it to the next compilation phase for processing. The most complicated one here is include. In fact, it is also very simple. It is equivalent to replacing the content in the corresponding file with the place of this include *** statement. The second step is compilation. This step is very important. compilation uses independent files as units, and a file will compile a target file. Here
Insert a note about the compiled file. The compiler uses the suffix to identify whether to compile the file. h. all the source files of cpp will be compiled. the suffix of the H file is changed. cpp, and change it to *** in the include field ***. cpp. In this way, the compiler will compile many unnecessary header files, but we usually only place declarations rather than definitions in the header files.
Therefore, the size of the executable file generated by the final link will not change) It is clear that the compilation is based on individual files, which is very important, therefore, compilation is only responsible for the tasks in this unit, and we ignore external tasks. In this step, we can call a function without having to give the definition of this function, however, you must obtain the declaration of this function before calling it.
In fact, this is the nature of include, not to provide you with a statement in advance so that you can use it? As to how the function is implemented, you need to find the function entry address in the Link step. Therefore, you can use include to include the Declaration in another file, or you can write a void max (int, int) statement before calling it .), The rest of the compilation phase is to analyze the correctness of the C ++ syntax. Well, to sum up, we can roughly think that the compilation phase is divided into two steps:
Step 1: Check whether functions or variables have their declarations;
Step 2: Check whether the statement conforms to the C ++ syntax. The last step is the link, which links all compiled units into a whole file. In fact, this step can be compared to a "Link" process,
For example, if file A uses A function in file B, the link will establish this association. When linking, I think the most important thing is to check whether there are duplicate or missing definitions in the global space. This explains why we generally do not define the header file, because the header file may be released to multiple source files, and each source file will be compiled separately, when linking, multiple definitions are found in the global space. Standard C and C ++ define the compilation process as nine Phases (Phases of Translation ):
1. Character Mapping)
The physical source characters in the file are mapped to the source character set, which includes replacement of the Three-character operator and replacement of the control characters (carriage return at the end of the line. Many non-American keyboards do not support some characters in the basic source character set. The file can be replaced by three characters ?? . However, if the keyboard is an American keyboard, Some compilers may not search for and replace the three characters. You need to add the-trigraphs compilation parameter. In the C ++ program, any character that is not in the basic source character set is replaced by its common character name.
2. Line Splicing)
The row ending with a backslash \ is merged with the following row.
3. Tokenization)
Each comment is replaced by a separate null character. C ++ double character operators are recognized as tags (to develop more readable programs, C ++ defines a set of double character operators and a new reserved word set for non-ASCII code developers ). The source code is analyzed as a preprocessing mark.
4. Preprocessing)
Call preprocessing commands and expand macros. Repeat steps 1 to 4 using the files contained in the # include command. The above four stages are collectively called preprocessing stages.
5. Character-set Mapping)
Source Character Set members and escape sequences are converted to equivalent execution Character Set members. For example, '\ a' is converted to a byte in the ASCII environment and the value is 7.
6. String Concatenation)
Adjacent strings are connected. For example, "" hahaha "" huohuohuo "will become" hahahahaohuohuo ".
7. Translation)
Compile the C ++ syntax and semantic analysis, and translate the code into the target code.
8. Process Template
Process Template instances.
9. Connection)
Solve the Problem of external reference and prepare the program image for execution.