C ++ compilation principles

Source: Internet
Author: User
Tags control characters

The first step is pre-compilation. In this step, we can roughly think that only one thing is done, that is, "macro expansion", that is, a kind of expansion of those # *** commands.

For example, define MAX 1000 is used to establish a peering relationship between Max and 1000. It is better to replace it in the compilation phase.

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. (Insert a description of the compiled file here. The compiler uses the suffix to identify whether to compile the file. Therefore, ". 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 last link will not change)

 

Clearly, compilation is based on individual files. This is very important. Therefore, compilation is only responsible for the tasks of this unit, and it ignores external tasks. In this step, we can call a function without defining it, but we need to get the declaration of this function before calling (in fact, this is the essence of include, isn't it just to give you 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 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 rows ending with a backslash/are merged with the following rows.

 

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 syntax and Semantic Analysis and translate it 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.