How to solve the problem of opening the pre-compiled header file and how the pre-compiled header works

Source: Internet
Author: User
How to solve the problem of opening the pre-compiled header file and how the pre-compiled header works

1. Use VC. Net to edit the program and press Ctrl + F7. the following error occurs:

Fatal error c1083: Unable to open the pre-compiled header file: "Debug/ugface. PCH": no such file or directory

Solution: Modify project-> properties-> C/C ++-> pre-compilation header-> do not use the pre-compilation header.

2. Open a previous project, compile it, and run it. I modified the Code a little (only modifying a parameter to change the integer written into the INI file from 0 to 10). At this time, the compilation failed and the header file "stdafx. H "indicates a compilation error (the error message is missing). The error location points to the statement" # include "stdafx of the source file of the application class. H "". It is not possible to include this file. I can't find a reason on the left and right, or even change the code back to the original one. Go to the network and check the following message:

When I learned to use the first routine of Visual C ++ 6.0, I got an error. after using the Wizard to generate the first project based on the dialog box, I followed the book ([VISUAL C ++ object-oriented and Visual Programming (second edition)] Tsinghua University Press pp76) the above example started my first program. When I entered the program one word per source code in the book, there was always an error: Fatal error c1010: unexpected end of file while looking for precompiled header directive. after countless times, I decided to include the header file statement: Include "stdafx. H "Reserved (and I deleted it before, because the example in the book does not contain this sentence .) okay. That's right. why? I am puzzled.

When I came to my VC source code directory, I noticed that the debug folder under each project is very large and the extension is. PCH occupies the vast majority of files, and I deleted it as if it had no effect on program compilation and running. so we held the right. curious about the PCH file. I found my questions online. this is what VC ++ 6.0 brings to us: Pre-compiled header files. 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. precompiled in project-> setting-> C/C ++'s category
Header, and then select to compile and generate 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... the following pre-compilation parameters refer to (/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.

 

In this case, I just need to re-compile "stdafx. cpp" to compile and run it. I don't know how happy I am, because I have encountered such a problem several times and I don't know how to solve it. I have to re-create an identical project.
I have encountered another problem before (solved For A Long Time): Create a project and find a CPP file. Press Ctrl + F7 and the system will prompt fatal error c1083: the pre-compiled header file cannot be opened: "Debug/xxx. PCH ": no such file or directory (where XXX is the project name) This is also the same reason, for VC stdafx. the H header file is not compiled. It can also be solved as follows: F7 first, then ctrf + F7 after compilation.
It seems that everything must start with the principle to be easier to solve.

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.