C + + precompiled header file (#include "stdafx.h")

Source: Internet
Author: User

Source: http://blog.sina.com.cn/s/blog_4ac766c00100qsbd.html

http://blog.csdn.net/txh0001/article/details/7031058

As a C + + rookie, in the precompiled header file (#include "stdafx.h") tangled for a long time, today intends to thoroughly understand it.

1. The concept of precompiled header files

The so-called precompiled header file, in fact, we are familiar with, here the header file (Microsoft Visual C + +) is generally said to be our common stdafx.h. The name is Microsoft default, the name can be changed, the content can be changed. This is the header file to be compiled, but, we know that the header file is not compiled, so we can use a stdafx.cpp, this file in the beginning can have no content, but must add a "include<stdafx.h>", Then a (. pch) file can appear compile (CTRL+F7), which is what we often call precompiled header files.

Why do I need precompiled header files? The reason is simple, and this is actually done to reduce compilation time. Because, if this is not the case, when compiling, if a header file is used by a lot of files, it is time-consuming, because it has to be compiled again and again. With the precompiled header file, we put the very high frequency of the thing (usually some of the system's header files or some of their own settings but infrequently changed header files) has been compiled, like a general-purpose parts, has been done, the use of the time directly loaded on the line. This makes it possible to take advantage of compiled results, which can save compilation time very effectively.

Precompiled header files in the 2.vs2008

Open Vs2008,file->new->project, set up a Win32 console application, select Next, Settings options in application additional: Precompiled header in the default check, click Finish, you can create a precompiled header file with the WIN32 console program. You can see that there are stdafx.h and stdafx.cpp in the Solution Explorer, and these two things are used to generate precompiled header files, which are also Microsoft's default.

    at this time compile, will find that is not successful, error is fatal error C1083:cannot open precompiled Header file: ' debugxxx.pch ': No such file or directory. It means that there is no. pch file. As mentioned above, in fact, the compilation process is the use of pre-compiled precompiled header files to reduce the amount of work compiled, so since there is no such precompiled header file, then how to compile it? We can right-click on the project in Solution Explorer, select the configuration properties->c/c++->preprocessor in the box on the right, and see the create/on the right. The Use Precompiled Header (/yu) is the default in the precompoled header, which means that it is not yet established. PCH, it started, obviously not suitable, then change, click on, select Create Precompoled Header (/yc), then you can. Compiled, you can see that there is a. pch in the Debug folder, which means the precompiled header file is done.

Go back to Create/use precompoled header page, see Create/use precompoled header below, Create/use PCH Through File is Stdafx.h, here or above, a compiler will default with this production precompiled header file, the production of the completion of the first ask where the price, the following item is precompoled header file is the address of the generated. PCH, whose default address is $ ( IntDir) $ (TargetName). PCH (where $ represents the current project folder, $ (IntDir) represents the current folder's debug folder, and the drop-down point edit can see address directories like Intdiroutir), generated in the debug directory, Name is (project name). pch. Here, you can change, you edit, the $ (TargetName). pch to PPP.PCH, compile, you can see the debug under a ppp.pch, this is a new precompiled header file, the system will be automatically added for you, and then automatically compiled with it. However, there is a point, just now that the precompiled header file did not disappear, but also quite large, a few m, that is, the original header file is not used, but did not cover out, then this reminds us to clean up the unused header files in time.

Another is that since already create a precompiled header file, then do not always create, we will create/use precompoled header in the change back to use Precompiled Header (/yu), Then you can save the compilation time.

3. What is the use of precompiled header files

As mentioned earlier, here I systematically summarize the following:

1) Some large programs use this better, but some small non-point program is not to use the precompiled header file is good, because the Create a precompiled header file itself is also time.

2) in the precompiled header file, the system header file is generally only placed. For example, you use MFC, a lot of header files must be used back and forth, then, do not worry about you, the system will default to you get a precompiled header file, to multiply the compilation time savings.

3) Some of the most commonly used basic fixed header files. In fact, I tend to do this, that is, we own a precompiled header file, with its own name, and then can be included in the system's Stdafx.h. Pay attention to this, and be sure to use a different name of your own. If you include your own head file in the Stdafx.h, the result will be the next (see the program people) confused, because we are accustomed to in their own head file to "another can kill 100, not let go of a" principle to include precompiled header files, if others to use this header file, then he can be miserable, The compiler will make this an ordinary file, it is not time-saving, it is a waste of time.

4, precompiled header file use experience:

If the precompiled header file is used correctly, it does greatly improve the efficiency of our programming (how much time do you have in your work and how long are you waiting for the compilation to complete?). Many of them, this time is generally very boring, helpless, wasting time. But he is too easy to use wrong. Here are a few common error usages.

1) Include your own header file in the precompiled header file

Cause: Your own head file will often change, convenient after the pre-compilation of the east to recompile, reduce the compilation speed, of course, if your header files do not change frequently, you can also

2) Also include precompiled header files in other header files

Assuming that your other header file also include the precompiled header file, if someone else references your header file is not set to a precompiled header file, the person who refers to your header file is suffering.

Cause: Because you used the. h file to include the precompiled header file, in his own project, vs can judge that he is the precompiled header, can also find the required PCH, PDB file. So the person writing this. h file has no effect. But you as his client, you work under your project, you include his h-header file, and at this time vs can't judge his header file include StdAfx is precompiled header file, do ordinary file compilation. It is conceivable that in his stdafx if there is an import outside the large library (such as the TLB of inventor, very slow, we made this mistake), the compilation speed is simply a torment. Most of all, you will have to re-edit any simple changes in the future, which is exactly the opposite of the problem with precompilation.

Here is a procedure to use a precompiled header file, and enjoy the compilation speed that the precompiled header file has brought to us:

1) Add a stdafx.h file (the name is random, here is the name of the default provided by the VS), in this. h file includes the header file to use (usually the external library, your own writing of the infrequently changed header file can also add in)

2) Add a stdafx.cpp file and include "StdAfx.h"

3) Project Properties-->c/c++-->precompiled set to use precompiled Header, stdafx.h

4) Stdafx.cpp Property-->c/c++->precompiled set to create precompiled Header, stdafx.h

done!

5, the use of precompiled headers

To use a precompiled header, we must specify a header file that contains the code and other header files that we will not change frequently, and then we use this header file to generate a precompiled header file (. pch file)

Presumably everyone knows StdAfx.h this file. Many people think that this is a "system level" provided by the VC, the compiler with a header file. Not really, this file can be any name. Let's examine a typical MFC generated by AppWizard

Dialog the precompiled header file for the Based program. (Because AppWizard will give us a good way to use the precompiled header file, the default is StdAfx.h, which is the name of VC). We will find this 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 <afxcmn.h>

These are the headers that must be included with MFC, and of course we are not likely to modify these header files in our project, so that they are stable.

So how do we specify it to generate precompiled header files. We know that a header file is not compiled. So we also need a CPP file to generate the. pch file. The default for this file is StdAfx.cpp. In this file, there is only one sentence of code:

#include "Stdafx.h". The reason is that we just want it to compile--that is, it's just the. cpp extension. We can use the/yc compile switch to specify StdAfx.cpp to generate a. pch file, compiled by/fp

Switch to specify the name of the generated PCH file. Open the Project->setting->c/c++ dialog box. Point the category to the precompiled Header. Select the entire project in the tree view on the left, and in the Diagram our project options (the white in the lower right corner

) can see/fp "debug/pch.pch", which is the name of the specified. PCH file, which is usually the default < project name >.pch

Then, in the tree view on the left, select StdAfx.cpp, and the original project option becomes the Source file option (originally a project, now a file, of course, changed). Here we can see the/yc switch, the role of/yc is to specify

This file to create a PCH file. The file name behind/yc is the header file that contains the stable code, and only one file can have a YC switch in a project. VC compiles StdAfx.cpp into an obj file and a PCH file based on this option.

In the command line, we can see/yu "stdafx.h"/fp "debug/depmodify.pch"

Then we choose another CPP file to see, here, Precomplier chose the use ..... One, the header file is the StdAfx.h file that we specified to create the PCH file. In fact, here is the use of the project settings, (

1)/yu "stdafx.h".

In this way, we set up the precompiled header file. In other words, we can use the precompiled header feature.

Here are some things to note:

1): If/yu is used, that is, pre-compilation is used, we start at the very beginning of each. cpp file (I emphasize that it is the beginning) contains the. h file that you specified to produce the PCH file (default is stdafx.h) or there will be a problem. If you do not include this file, you will be told

V. You unexpected file end. If you are not at the beginning of the inclusion, you have to try the following to know, there is absolutely amazing effect ...

2) If you accidentally lost the PCH file, according to the above analysis, you just have to let the compiler generate a PCH file. That is, the stdafx.cpp (that is, the CPP file specified/yc) from the new compilation again. Of course you can be silly Rebuild.

All The simple point is to select the CPP file, click CTRL + F7 on it.

C + + precompiled header file (#include "stdafx.h")

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.