9 phases of C ++ Compilation

Source: Internet
Author: User

When developing a C ++ program, the first problem encountered is the issue of C ++ compilation. It is compiled through the compiler, And the compiler finally identifies whether to compile the file through the extension name, therefore, ". h.

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. C ++ compilation uses independent files as units, and a file will compile a target file. Insert a note about the compiled file here. The compiler uses the suffix to identify whether to compile the file. Therefore, the header file ". h" is ignored.

And ". 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. It is clear that C ++ compilation is based on individual files, which is very important, therefore, compilation is only responsible for the tasks of the current unit, but does not care about external tasks. In this step, we can call a function without having to define the function.

  • Notes for handling C ++ static members
  • How to construct C ++ virtual base class Constructor
  • Easily solve C ++ exceptions
  • Summarize stack objects referenced in C ++/CLI
  • Interpretation of the C ++ compiler compilation function

But to get the declaration of this function before calling, this is actually the essence of the include. Isn't it just to provide you with a declaration 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 C ++ compilation stage is divided into two steps: the first step is to check whether the functions or variables have their declarations; the second step, 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.

The standard C and C ++ compilation processes are defined as nine Phases (Phases of Translation ):

1. Physical source characters in the Character Mapping File are mapped to the source Character set, including replacement of the three Character operator and replacement of the control Character (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 merges rows ending with backslash \ with the following lines.

3. Each annotation of Tokenization 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 calls Preprocessing commands and expands 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 ter ing (Character-set Mapping) Source Character set members and escape sequences are converted into 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. Compile the syntax and Semantic Analysis in C ++ and translate it into the target code.

8. Process Template processing template instances.

9. The connection solves the problem of external reference and prepares the program image for execution.

  1. Introduction to C ++
  2. Summary Notes on learning and exploring C ++ library functions
  3. Basic Conception and method of C ++ Class Library Design
  4. Does C ++ really have market value?
  5. Basic Conception and method of C ++ Class Library Design

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.