) Pre-compiled header files

Source: Internet
Author: User

Today, I am making a huge change.Program. Suddenly I found a. c file with nothing in it,

Just a few header files, I think, I rely on it. Isn't it complicated to solve the simple problem? Just delete the c file.

The results cannot be compiled. I rely on the following:

Fatal error c1083: cannot open precompiled header file: \ 'debug/v13_3.pch \':

No such file or directory

No way to rebuild all.

I checked it online to understand it:

---------------- Summary ------

If the project is large, there are many header files, and several header files are frequently used

1. Write all these header files to a header file, such as preh. h.

2. Write a preh. C with only one sentence: # include "preh. H"

3. For preh. C, set creat precompiled headers in project setting. For other

. C file, set the use precompiled header file

//

Haha

I tried it. The effect is very obvious. I don't need precompiled headers. Once compiled, I can go to the toilet and use

Precompiled header. during compilation, I can stand up and stretch out, and the activity is almost the same.

--------- ReprintedArticle----------

Concepts of precompiled headers:

The so-called pre-compilation header refers to the part of a project.Code, Pre-compiled and put in a file (usually

With the. PCH extension), this file is called the pre-compiled header file. The pre-compiled code can be any

C/C ++ code -------- or even inline functions, but must be stable, not during project development

Changed frequently. If the code is modified, re-compile the pre-compiled header file. Note that pre-editing is generated.

It takes a lot of time to translate the header file. At the same time, you must note that the pre-compiled header file is usually large, usually 6-7 M. Note and

To clear unused pre-compiled header files.

You may ask: The current compiler has the time stamp function. When the compiler compiles the entire project, it

Only the modified files will be compiled, and those files will not be compiled from the previous compilation, but have not been modified yet.

. So why do we need to pre-compile the header file? Here, we know that the compiler is a file

After a file is modified, the entire file will be re-compiled. Of course, all

The header file (. eg macro, preprocesser) must be processed again. Pre-compiled header file of VC

This information is saved. To avoid re-processing these header files every time.

Precompiled headers:

According to the above introduction, the role of pre-compiled header files is of course to improve the speed of cheap, with it you do not need

Compile the code that does not need to be changed frequently. Of course, the compilation performance is improved.

Use of pre-compiled headers:

To use the pre-compiled header, we must specify a header file which will not be changed frequently.

Code and other header files, and then we use this header file to generate a pre-compiled header file (. PCH file)

We all know the stdafx. h file. Many people think this is a "system-level" provided by VC.

, A header file carried by the compiler. Actually not. This file can be of any name. Let's look at

A typical pre-compiled header file of the MFC dialog based program generated by Appwizard. (Because Appwizard

We will specify how to use the pre-compiled header file. The default value is stdafx. H, which is the name of Vc ). We

The 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 <afxcen. h>

These are the necessary header files used by MFC. Of course, we are unlikely to modify these headers in our project.

So they are stable.

How can we specify it to generate the pre-compiled header file. We know that a header file cannot be compiled. So I

We also need a CPP file to generate the. PCH file. The default value of this file is stdafx. cpp. In this file

There is only one code: # include "stdafx. H ". The reason is taken for granted. We just want it

Compilation is enough? D? D? D. That is to say, only the extension of. cpp is needed. We can use the/YC compilation switch to specify

Stdafx. cpp is defined to generate a. PCH file. The/FP compilation switch is used to specify the name of the generated PCH file. Dozen

Open the project-> setting-> C/C ++ dialog box. Point category to the precompiled header. On the left

Select the entire project in the Tree View

Project Options (the white place in the lower right corner) can see/FP "Debug/PCH. PCH", which means

Specifies the name of the generated. PCH file. The default value is usually <Project Name>. PCH (my example project name is PCH ).

Then, select stdafx. cpp in the Tree View on the left. // select only one CPP file!

At this time, the original project option becomes the source file option (originally a project, now a file

). Here we can see that the/YC switch is used to specify this file to create

PCH file. The file name after/YC is the header file containing the stable code. Only one file can be entered in a project.

The YC switch can be enabled for parts. VC compiles stdafx. cpp into an OBJ file and a PCH file based on this option.

.

Then we can select another file to see, // other CPP files

Here, precomplier selects the "Use your own keys" option. The header file is the stda that we specified to create the PCH file.

FX. h

File. In fact, the settings in the project are used here, (1)/Yu "stdafx. h ".

In this way, we have set the pre-compiled header file. That is to say, we can use the pre-compiled header function. To

Note:

1): If/Yu is used, that is, pre-compilation is used. We will emphasize it at the beginning of each. cpp file.

Is the beginning, contains the. h file you specified to generate the PCH file (default is stdafx. h) otherwise there will be problems. For example

If you do not include this file, you will be notified of the unexpected file end. If you do not include it at the beginning,

You can try the following on your own. It is absolutely amazing ..

Fatal error c1010: unexpected end of file while looking for precompiled

Header Directive

Generating code...

2) If you accidentally lose the PCH file, many abnormal behaviors will occur during compilation. Based on the above

You only need to let the compiler generate a PCH file. That is to say, set stdafx. cpp

CPP file. Of course, you can rebuild all. Simply select the CPP

File. Press Ctrl + F7. Otherwise, it would be a waste of time.

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.