Parsing an in depth analysis of precompiled header files in VC

  • 2020-04-01 23:39:07
  • OfStack

one Why precompile header files:
Precompiled header concept:
The precompiled header is a project of that part of the code, precompiled well in a file (usually in. PCH for extension), the file is called a precompiled header file these previously compiled code can be any C/C + + code, and even the inline function, but must be stable, in the process of project development will not be constantly changing. If the code is modified, you need to recompile to generate the precompiled header file. Note that generating a precompiled header file is time consuming. Also note that precompiled headers are usually large, usually 6-7m. Be careful to clean up any unused precompiled headers.
You may ask: the current compiler has the function of Time stamp. When the compiler compiles the whole project, it will only compile the files that have been modified, and will not compile the files that have not been modified since the last compilation. So why precompile a header file? The answer here is that the compiler compiles on a file basis, and when a file is modified, it recompiles the entire file, and of course everything in the header file included in the file (.eg Macro, Preprocessor) is reprocessed. VC's precompiled header file holds exactly this information. To avoid having to reprocess these header files every time.
According to the above, the purpose of a precompiled header is of course to speed up the compilation, because you don't have to compile code every time that doesn't need to be changed very often. Compilation performance certainly improves.

Let's look at a precompiled header file for a typical MFC Dialog Based program generated by AppWizard. Because the AppWizard will specify for us how to use the precompiled header file, the default is stdafx.h, which is the name of the 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 < Afxcmn. H >
These are the header files that must be included to use MFC, and of course we are unlikely to modify them in our project, so they are stable.

two How to precompile header files:
To use a precompiled header, we have to specify a header file that contains code that we don't change very often and other headers, and then we use that header file to generate a precompiled header file (.pch file) that you probably know about stdafx.h. Many people think of this as a "system-level" compiler header provided by a VC. No, it can be anything.
1. How to use precompiled header files in the project: the easiest way is to select "use precompiled header files" when building the project; If you don't choose to build the project, you can also choose project - > Setting - > C/C + + - > Set in the precompimed header.
2. How to specify the header file name of a precompiled header file: stdafx.h by default, but you can use project - > Setting - > C/C + + - > Set to any name in the precompimed header (actually by directive /Yc, /Yu). Then include the header file of the stable code 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.
4. Through project - > Setting - > C/C + + - > The precompimed header sets the name of the precompiled file (ending in.pch) (actually by specifying /Fp).

3. How to use precompiled header files:
1. Through project - > Setting - > C/C + + - > The precompimed header is set to use the precompiled header file (/Yc, /Yu), which header file to rely on, and the precompiled header file name.
2. If you accidentally lose a PCH file, based on the above analysis, you can simply ask the compiler to generate a PCH file. This means that stdafx.cpp (the CPP file that specifies /Yc) can be recompiled. Of course you can foolishly Rebuild all. Simply select the CPP file and press Ctrl + F7.
3. If /Yu is used, that is, pre-compiled, we are at the beginning of each. CPP file, which I repeat is the beginning, including the. H file you specified to produce the PCH file (the default is stdafx.h) otherwise there will be a problem. If you don't include this file, tell you the Unexpected file end. If you don't include it at the beginning, try this for yourself. The reason is that the compiler does not compile the header file before stdafx.h when it compiles.

Related articles: