Turn: Pre-compiled header files and compilation options

Source: Internet
Author: User

Pre-compiled header files (generally with the extension. PCH) are precompiled into a file (. PCH) for the stable code in a project. 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 are relatively stable. Re-compiling all files of the entire project for a single small file leads to a reduction in compilation efficiency, so the. PCH file is introduced.

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 ". Next, use it to generate the. PCH file, which involves several important pre-compilation commands:/YU,/YC,/Yx,/FP. Simply put:

/YC: The compile switch used to generate the. PCH file. 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 Indicates create ).

/FP: Specifies the name and path of the generated. PCH file (possibly/fp p indicates path ).

The U of/Yu is used for use. This/YU command is available in files that contain. H files in the project. If Automatic automatic... is selected, the original location of/YC is replaced with the/Yx command. If Automatic is selected, the compiler will check whether the. PCH file has been generated before each compilation. If yes, The. PCH file will be compiled again.

Note:

A: Actually, the default header files and CPP files stdafx. h and stdafx. cpp generated by the appzard Project Wizard can be named by any name. The reason is simple, but if you want to do so, remember to modify the corresponding project-> setting... parameters of the following pre-compiled commands (/YC,/YU, YX,/FP.

B: 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 (Pass. 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. the PCH file does not need to be dumbly pressed by F7 or rebuild all.

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.