1 phenomena
When using the VC + + Application Wizard generated source framework, whether it is an MFC application, or the Win32 form program, will default to enable the "Precompiled Header" feature, automatically generate stdafx.h and stdafx.cpp two files. When you add a source file to your project later, the precompiled Header feature is automatically enabled for that source file. Requirements must be added at the beginning of the. cpp file #include "stdafx.h", or the compiler will error: fatal error C1010: Encountered unexpected end of file when looking for precompiled headers. Did you forget to add "#include" stdafx.h "" to the source?
2 Implementation of precompiled header function
The source files that participate in the precompiled header feature are divided into three types:
(1) (indicator header file) Pre-compiled header indicator files, such as common stdafx.h;
(2) (create/yc) The source file of the precompiled result, such as common stdafx.cpp;
(3) (use/yu). cpp file with "precompiled header"; The file must be included, #include "stdafx.h".
It is important to note that any source file may be one of the above three roles, the file name is not fixed to stdafx.h and stdafx.cpp, but can be modified at will. However, when used in practice, the default settings are often used.
VC + + When compiling the project:
(1) First compile the source file that produces the precompiled header, and compile the result is the projectname.pch file;
(2) Then compile other. cpp files, if a. cpp file uses the precompiled header feature, then when compiling it, first look for the precompiled header to indicate the line. If it is not found, it will error, if found, it will not be like other header files included in the compilation, but directly using PROJECTNAME.PCH to participate in the CPP file compilation;
3 Enable and disable the "Precompiled Header feature"
The precompiled header is a feature of the VC + + compiler, which can be enabled or disabled within the scope of the project, or can be individually enabled or disabled for each source file.
3.1 Enabling and disabling the precompiled header at the project level
If you use the VC + + Application Wizard to generate a WIN32 or an MFC project framework, the precompiled header feature is enabled by default on the project. If you want to disable this feature after the project is established, you need to set it in the project properties, as shown in:
You can disable the precompiled header feature by selecting "Do not use" here. Once disabled at the project level, all source files that originally use precompiled headers will no longer use precompiled headers.
3.2 Enabling and disabling the precompiled header at a single source file level
Sometimes when the project is built with "Precompiled header feature" enabled, but then other third-party source code is imported, these source code files will automatically enable the "Precompiled Header" feature on import, but these source files do not contain #include "stdafx.h" line, so compile will be error. There are two workarounds, one for each source file, and the second is to disable the precompiled Header feature for the source file. To disable the precompiled Header feature for a CPP file individually, simply right-click it in VS and open the Properties dialog box, where you can modify it. As shown in the following:
4 The origin of "Precompiled header"
At compile time, the header file is not directly involved in compilation, but is merged with the CPP file when it is preprocessed and then participates in the compilation. Some header files are required to be included in almost all CPP files, such as <stdio.h>,<windows.h>, so that these files need to be compiled repeatedly in each CPP file that contains it. Earlier, these header files were small, and it was not much time to repeat the compilation. However, when MFC appears, its header files become super large, such as a <afxwin.h> for more than 6,000 lines, so repeatedly compiling these long header files will result in longer compilation time. In order to solve this problem, VC + + compiler to compile these common header files in advance, and then compile the CPP file containing it, directly using the header file compilation results to participate in the CPP file subsequent compilation tasks. This "one-compile, multiple-use" approach can actually reduce compilation time.
5 My Choicesimple use of VC + + development projects, the use of "precompiled head" function, there is no harm to profit. However, sometimes, our source files need to implement cross-platform, and the compiler outside VC + + does not support this feature, it is necessary that these source files separately disable this feature. When you integrate third-party source code into VC + + projects, you also need to disable the "Precompiled Header" setting (VS support to select multiple source files simultaneously while enabling/disabling the precompiled header), of course, if not too troublesome, you can also add #include "stdafx.h" line at the beginning of each source file.
VC + + Compiler pre-compiler header function (stdafx.h) principle and practice