In-depth analysis of pre-compiled header files in VC

Source: Internet
Author: User

I. Why 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.
According to the above introduction, the role of the pre-compiled header file is of course to improve the compilation speed. 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.

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.

II. How to pre-compile the header file:
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 StdAfx. h. 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.
1. how to use the pre-compiled header file in the project: The simplest is to select "use pre-compiled header file" when creating the project; if the pre-compiled header file is not selected during project creation, you can also set it in project-> Setting-> C/C ++-> precompimed header.
2. how to specify the header file name of the precompiled header file: The default value is StdAfx. h, but you can set any name in the project-> Setting-> C/C ++-> precompimed header (actually by command/Yc,/Yu ). Then, you can include the stable and unchanged code header file in the header file.
3. required implementation file: the CPP file is required for compilation, and this CPP file is naturally the CPP file corresponding to the header file name. In this CPP file, there is only one sentence: contains the file header to be precompiled.
4. Use the project-> Setting-> C/C ++-> precompimed header to set the name of the pre-compiled file (ended with. pch) (actually by specifying/Fp ).

3. How to use pre-compiled header files:
1. use the project-> Setting-> C/C ++-> precompimed header to set the header file to which the precompiled header file (/Yc,/Yu) depends, and pre-compiled header file names.
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 recompile 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.
3. 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. (The reason is that the header file before stdafx. h is not compiled by the compiler ).

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.