C + + precompiled header File

Source: Internet
Author: User

http://blog.csdn.net/btooth/article/details/980251

Many beginner VC friends may have been plagued by a problem:

Why do all CPP have to #include "stdafx.h"

Perhaps after consulting another master, they will tell you that this is precompiled header that must be included. But, what is this
is why. What's the use of precompiled headers?

This has to start from the compiler principle of the document. In fact, the head of the file is not mysterious, its full role is to put their own
All content is "pasted" directly into the corresponding #include statement. If you do not believe, you might as well do an experiment that will
All #include statements in a CPP are deleted and the files it contains are pasted into the appropriate location, and you will send
Now, the compilation and operation of the file are completely unaffected. In fact, when compiling your program, the compiler does
The first thing is to expand all #include statements and #define statements.

The appearance of the header file, of course, has brought great convenience to the writing program. But after the Windows age, slowly
It presents some problems. Almost all Windows programs must contain windows.h, and that file is
Gigantic, it expands to all the files in a paste, compile the time immediately as slow as a snail.

After the MFC era, the situation is even worse. After all, the C-style Windows header file contains
only function definitions and macros, the compilation is not too difficult, and the MFC library header file can be a class declaration AH. More
What's more, one of the simplest projects will generate a large number of classes that require a large number of functions. If the project is slightly complicated
Some, the compilation difficulty is conceivable.

However, people are surprised to find that, although the use of the header files are many and miscellaneous, but in a project, there are always so
A bunch of header files that almost any CPP must contain. So, can you extract these headers,
Compile only one, and then all other CPP will be available. Yes, that's what the idea of precompiled headers is all about.

It has been proved by practice that the compiler speed is greatly improved after using the precompiled header technique. You can go to your engineering directory.
Debug or Release directory Look, there is a very large size. pch file, that's the legend.
Compile precompiled headers in.

The use of precompiled-header technology, while great convenience, has created a problem: because it assumes
The header file contained in the precompiled header is used in all CPP, so when compiling your CPP, it will
Loads the compiled portion of the precompiled header into memory. If it suddenly finds out that your CPP doesn't contain
Precompiled header, it would be very depressing because it doesn't know how to get the compiled parts out of memory, the whole
The compilation process will fail.

Therefore, if you use Precompiled header technology, you must include precompiled headers in all of the CPP. MFC work
You have set up a default precompiled header for you stdafx.h, if you like, you can also in your own project to make
Use a different filename as your precompiled header if you feel it is necessary.

Use of precompiled header files
Keywords: precompiled,/yu,/yc,/yx
This article describes the use of VC6, because of the complexity of using precompilation in detail, here are just a few of the most important precompiled directives:/yu,/yc,/yx,/fp. Other detailed information can be referred to as:
Msdn->visual Studio d6.0document-> Visual c++6.0 Document
->vc++ programmer Guider->compiler and Linker
->details->creating Precompiled Header Files

The concept of precompiled headers:
The so-called precompiled header is to put that part of the code in a project, pre-compiled and placed in a file (usually with a. pch extension), this file is referred to as a precompiled header file These precompiled code can be any C/s + + code--------even inline functions, But it must be stable and will not be constantly changed in the process of engineering development. If the code is modified, you will need to recompile the build precompiled header file. Note Generating precompiled header files is time-consuming. You should also note that precompiled headers are usually large, usually 6-7m. Be careful to clean up precompiled headers that are not used in time.
You may ask: Now the compiler has time stamp function, the compiler compiles the entire project, it will only compile those modified files, and not to compile those from the last compiled, to now have not been modified files. So why do you have to precompile the header file? The answer is here, we know the compiler is compiled in file, a file is modified, will recompile the entire file, of course, all the headers contained in this file (. eg Macro, preprocessor) should be processed again. VC's precompiled header file saves exactly this part of the information. To avoid having to process the header files each time.
The role of precompiled headers:
Method One: Manual method
As described above, the role of precompiled headers, of course, is to increase the speed of cheap, with it you do not have to compile every time the code that does not need to change frequently. The compilation performance is certainly improved.
Use of precompiled headers:
To use a precompiled header, we must specify a header file that contains 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)
We must all know StdAfx.h this document. Many people think that this is a "system level" provided by VC, a compiler with a header file. Not really, this file can be any name. Let's examine a typical precompiled header file of an MFC Dialog Based program generated by AppWizard. (Because AppWizard will specify for us how to use Precompiled header file, the default is StdAfx.h, this is the name of VC). We will find that 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 <afxcmn.h>
These are the headers that must be included with MFC, and of course we are not likely to modify these headers in our project, so they are stable.
So how do we specify it to generate the precompiled header file. We know that a header file is not compiled. So we also need a CPP file to generate the. pch file. The default of this file is StdAfx.cpp. In this file there is only one code: #include "Stdafx.h". The reason is, we just want it to be able to compile--that is to say, only the extension of its. cpp. We can use the/yc compiler switch to specify STDAFX.CPP to generate a. pch file, and to specify the name of the generated PCH file by/fp the compile switch. 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 (figure)
(Figure 1)
In the diagram, our project Options (the white place in the lower right corner) can see/fp "debug/pch.pch", which is the name of the generated. PCH file, and the default is usually the < engineering name &GT;.PCH (my example project name is PCH).
Then, select StdAfx.cpp in the tree view on the left. Figure: (Figure 2)
At this point the original project option became Source file option (originally a project, now a file, of course it changed). Here we can see the/yc switch, the/yc function is to specify this file to create a PCH file. The filename behind the/yc is the header file that contains the stable code, and only one file can have a YC switch in a project. VC based on this option to compile the StdAfx.cpp into an obj file and a PCH file.
Then we'll select a different file to look at, as shown in figure:
Here, Precomplier chose to use .... One, the header file is the stdafx.h that we specified to create the PCH file
File. In fact, here is the use of engineering settings, (Figure 1)/yu "stdafx.h".
That 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, which means using precompilation, we start at the very beginning of each. cpp file, and I emphasize that it is the very beginning, including the. h file that you specified to generate the PCH file (default is stdafx.h) Otherwise there will be a problem. If you don't include this file, tell you unexpected file end. If you don't include it at the beginning, you'll know it by yourself, and it definitely has a very amazing effect ...
2 If you accidentally lost the PCH file, according to the above analysis, you just let the compiler to generate a PCH file on it. That is, the stdafx.cpp (that is, the designated/yc CPP file) can be compiled again. Of course you can be silly Rebuild all. The simple point is to select the CPP file, click CTRL + F7 on it.
Method Two. Automatically use
It's easy to just specify/YX. Or in the above figure, select Automatic ... It's OK. The thing to note is that if you specify/yc/yu,/YX will be ignored. The former has a higher priority level.

Related Article

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.