Precompiled header file (the general extension is. PCH) is to compile the more stable code in a project in advance and put it in a file (. PCH. these pre-compiled codes can be any C/C ++ code-or even inline functions, but they are relatively stable throughout the project, that is, the code that is not frequently modified during the project development process.
Why do we need to pre-compile the header file? In a word: it increases the compilation speed. generally, the compiler compiles files in units. If a file in a project is modified, all files must be re-compiled, including everything in the header file (eg. macro macro, Preprocessor preprocessing), while in VC programs, the content contained in these header files is often very large, and compilation takes a long time. but they are not often modified, and they are relatively stable. Re-compiling all files of the entire project for a single small file leads to a reduction in compilation efficiency, which is introduced. PCH file.
How to use pre-compiled header files to increase Compilation speed? To use a precompiled header file, you must specify a header file (. h), it contains the code that we do not often modify and other header files, and then use this header file (. h) generate a pre-compiled header file (. PCH) the default header file of VC is stdafx. h. Because the header file cannot be compiled, we still need one. CPP file as a bridge. The default VC file is stdafx. CPP, the only code in this file is: # include "stdafx. H ". use it to generate. the PCH file involves several important pre-compilation commands:/YU,/YC,/Yx,/FP. in short,/YC is used to generate. PCH file compilation switch. in the category of project-> setting-> C/C ++, select the precompiled header for compilation in the Tree View on the left. the. CPP file (default: stdafx. CPP) you can see the/YC switch, which indicates whether the file is generated after compilation. PCH file (possibly/YC c Represents create ). /FP command to specify the generated. the name and path of the PCH file (maybe/FP p represents path ). /Yu's U is used for use, as long as it is included in the project. this/YU command is available for all files in the H file. if you select automatic... the original/YC is replaced with the/Yx command. if Automatic is selected, the compiler will check whether it has been generated before each compilation. PCH file. If yes, it will not be generated. Otherwise, it will be compiled again. PCH file.
Note:
A. In fact, the default header file and CPP file stdafx generated by the appzard Project Wizard. H and stdafx. CPP can be any name. the reason is simple. but if you want to do this, remember to modify the corresponding project-> setting... parameters of several pre-compiled commands (/YC,/YU,/Yx,/FP.
B. it is used in any header file that includes the header file to be precompiled. the start of the project file of the PCH file must be at the beginning. You must include the specified file to be generated. the. h file (. the CPP file is included. The default value is stdafx. CPP), if not included, it will generate the first error. if it is not at the beginning, it will lead to unexpected and inexplicable errors. If you do not believe it, should it be a try?
C. pre-compiled file. PCH generation takes a lot of time, and it also occupies disk space after generation, usually 5-6 M. Note that it is useless to clean up after the project is completed. PCH file to save disk space.
D. if it is lost or deleted. when you need to modify the project file later. CPP file (default: stdafx. CPP) re-compile once to generate again. PCH file. Do not use F7 or rebuild all.
If you use the vs series compiler, you can set PCH in the project properties. Other compilers should be similar, but you have not tried it ..~~