C ++ pre-compiled header file

Source: Internet
Author: User
(I would like to express my gratitude to the author of the original article for his hard work. If the post has damaged the interests of the author, please contact me and I will delete the article .)
Original in http://www.rayoko.com/article/201.htm
The content of this article is as follows:

Why must all CPP # include "stdafx. H"
After asking other experts, they will tell you that this is a pre-compiled header that must be included. But why? What is the use of precompiled headers?
This must start with the Compilation Principle of the original file. In fact, the header file is not mysterious. Its full function is to "Paste" all its content to the corresponding # include statement. If you do not believe this, you may wish to create an experiment to delete all the # include statements in a CPP and paste the files contained in it to the corresponding location. You will find that, the compilation and running of files are completely unaffected. In fact, the first thing the compiler does when compiling your program is to expand all the # include statements and # define statements.
The appearance of header files makes writing programs very convenient. However, in the Windows era, some problems have gradually emerged. Almost all Windows programs must contain windows. h, but the file is huge. After it is expanded and pasted into all files, it immediately slows down as a snail bait during compilation.
In the MFC era, the situation is even worse. After all, the C-style Windows header file contains only function definitions and macros. Compilation is not too difficult, and the header files in the MFC library can all be class declarations! What's more, a simple project generates a large number of classes and requires a large number of functions. If the project is a little more complex, the compilation difficulty can be imagined! However, it is surprising that, although many header files are used, there is always a pile of header files in a project that almost all CPP files must contain. So, can I extract these header files, compile only one compilation, and use all other CPP files? That's right. This is the idea of precompiled headers!
Practice has proved that the compilation speed is greatly improved after the pre-compiled header technology is used. You can go to the debug or release directory in your project directory and check out the huge. PCH file, which is the legendary "precompiled header ".
After the pre-compilation header technology is used, although it brings great convenience, it also causes a problem: Because it assumes that the header files included in the pre-compilation header will be used in all CPP, therefore, when compiling your CPP, it will load the compiled part of the pre-compilation header into the memory. If it suddenly finds that your CPP does not contain the pre-compilation header, it will be very depressing because it does not know how to extract the compiled part from the memory, the entire compilation process will fail.
Therefore, if you use the pre-compilation header technology, you must include the pre-compilation header in all CPP files. A default pre-compilation header stdafx is created for you in the MFC project. h. If you like, you can also use other file names in your project as your pre-compilation headers if you think it is necessary.
Use of precompiled header files
Keywords: Pre-compiled,/YU,/YC,/Yx
This article describes how to use the pre-compilation function of vc6. Due to the complicated usage of pre-compilation, only the most important pre-compilation commands:/YU,/YC,/Yx, /FP. For more information, see:
Msdn-> Visual Studio d6.0document-> visual c ++ 6.0 document
-> VC ++ programmer guider-> compiler and linker
-> Details-> creating precompiled header files
Concepts of precompiled headers:
The so-called pre-compilation header is to pre-compile the part of the code in a project and put it in a file (usually. PCH Extension). This file is called the precompiled header file. The pre-compiled code can be any C/C ++ code -------- or even an inline function, however, it must be stable and won't be changed frequently during project development. If the code is modified, re-compile the pre-compiled header file. Note that it takes a lot of time to generate a pre-compiled header file. At the same time, you must note that the pre-compiled header file is usually large, usually 6-7 M. Clear unused pre-compiled header files in time.
You may ask: The current compiler has the time stamp function. When the compiler compiles the entire project, it only compiles the modified files, instead of compiling files that have not been modified since the previous compilation. So why do we need to pre-compile the header file? Here, we know that the compiler is compiled in files. After a file is modified, it will re-compile the entire file. Of course, all the header files contained in this file (. eg macro, Preprocessor) must be processed again. The pre-compiled header file of VC stores this part of information. To avoid re-processing these header files every time.

Precompiled headers:
Method 1: Manual
According to the above introduction, the role of pre-compiled header files is of course to improve the cost efficiency. With it, you do not need to compile the code that does not need to be changed frequently every time. Of course, the compilation performance is improved.
Use of pre-compiled headers:
To use the pre-compiled header, we must specify a header file, which contains code that we will not change frequently and other header files. Then we use this header file to generate a pre-compiled header file (. PCH file)
We all know the stdafx. h file. Many people think that this is a system-level file provided by VC, a header file carried by the compiler. Actually not. This file can be of any name. We will examine a typical pre-compiled header file of the MFC dialog based program generated by Appwizard. (Because Appwizard specifies how to use the pre-compiled header file. The default value is stdafx. H, which is the name of VC ). We will find that this header file contains the following header files:
# Include <afxwin. h> // MFC core and standard components
# Include <afxext. h> // MFC extensions
# Include <afxdisp. h> // MFC automation classes
# Include <afxdtctl. h> // MFC support for Internet Explorer 4 common controls
# Include <afxcen. h>
These are the necessary header files used by MFC. Of course, we are unlikely to modify these header files in our project, so they are stable. Then how do we specify it to generate the pre-compiled header file. We know that a header file cannot be compiled. Therefore, we need a CPP file to generate the. PCH file. The default value of this file is stdafx. cpp. There is only one code in this file: # include "stdafx. H ". The reason is, of course, we only need to compile it-that is to say, it only needs its. cpp extension. We can use the/YC compilation switch to specify stdafx. cpp to generate a. PCH file, and use the/FP compilation switch to specify the name of the generated PCH file. Open the project-> setting-> C/C ++ dialog box. Point category to the precompiled header. Select the entire project in the Tree View on the left
In the figure, we can see/FP "Debug/PCH" in the project options (the white place in the lower right corner. PCH ", which is generated by the specified. the name of the PCH file. The default value is usually <Project Name>. PCH (my example project name is PCH ). then, select stdafx in the Tree View on the left. CPP. at this time, the original project option becomes the source file option (originally a project, now a file, of course changed ). Here we can see the/YC switch. The function of/YC is to specify this file to create a PCH file. The file name after/YC is the header file that contains stable code. Only one file in a project can have the YC switch. VC compiles stdafx. cpp into an OBJ file and a PCH file based on this option.
Then we can select another file to see:
Here, precomplier chooses use ......... Header file is the stdafx. h file that we specified to create the PCH file. In fact, here we use the settings in the project, Yu "stdafx. h"
In this way, we have set the pre-compiled header file. That is to say, we can use the pre-compiled header function. Note:
1): If/Yu is used, that is, pre-compilation is used. the beginning of a CPP file. I emphasize that it is the beginning of a file, including the file that you specified to generate a PCH file. h file (default is stdafx. h) otherwise there will be problems. If you do not include this file, you will be notified of unexpected file end. If you do not include it at the beginning, you can try the following and you will find it amazing .....
2) If you accidentally lose the PCH file, you only need to let the compiler generate a PCH File Based on the above analysis. That is to say, you can re-compile stdafx. cpp (that is, the CPP file of the specified/YC. Of course, you can rebuild all. Simply select the CPP file and press Ctrl + F7.
Method 2: automatic use
You only need to specify/Yx. Or Select automatic ......... You can. Note that if you specify/YC/YU,/Yx will be ignored. The former has a higher priority.

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.