Use of precompiled header files

Source: Internet
Author: User

Article Name:Use of precompiled header files
Outbound: Internet
Author: Pan lilang

This article describes how to use the pre-compilation function of vc6. Due to the complicated usage of pre-compilation, only the most important pre-compilation commands:/YU,/YC,/Yx, /FP. For more information, see:
Msdn-> Visual Studio d6.0document-> visual c ++ 6.0 document
-> VC ++ programmer guider-> compiler and linker
-> Details-> creating precompiled header files

Concepts of precompiled headers:

The so-called pre-compilation header is to pre-compile the part of the code in a project and put it in a file (usually. PCH Extension). This file is called the precompiled header file. The pre-compiled code can be any C/C ++ code -------- or even an inline function, however, it must be stable and won't be changed frequently during project development. If the code is modified, re-compile the pre-compiled header file. Note that it takes a lot of time to generate a pre-compiled header file. At the same time, you must note that the pre-compiled header file is usually large, usually 6-7 M. Clear unused pre-compiled header files in time.

You may ask: The current compiler has the time stamp function. When the compiler compiles the entire project, it only compiles the modified files, instead of compiling files that have not been modified since the previous compilation. So why do we need to pre-compile the header file? Here, we know that the compiler is compiled in files. After a file is modified, it will re-compile the entire file. Of course, all the header files contained in this file (. eg macro, Preprocessor) must be processed again. The pre-compiled header file of VC stores this part of information. To avoid re-processing these header files every time.

Precompiled headers:

Method 1: Manual

According to the above introduction, the role of pre-compiled header files is of course to improve the cost efficiency. With it, you do not need to compile the code that does not need to be changed frequently every time. 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 contains code that we will not change frequently and other header files. 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 that this is a system-level file provided by VC, a header file carried by the compiler. Actually not. This file can be of any name. We will examine a typical pre-compiled header file of the MFC dialog based program generated by Appwizard. (Because Appwizard specifies how to use the pre-compiled header file. The default value is stdafx. H, which is the name of VC ). We will find that 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 <afxcen. h>

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

Then how do we specify it to generate the pre-compiled header file. We know that a header file cannot be compiled. Therefore, we need a CPP file to generate the. PCH file. The default value of this file is stdafx. cpp. There is only one code in this file: # include "stdafx. H ". The reason is, of course, we only need to compile it -- that is to say, it only needs the extension of its. cpp. We can use the/YC compilation switch to specify stdafx. cpp to generate a. PCH file, and use the/FP compilation switch to specify the name of the generated PCH file. Open the project-> setting-> C/C ++ dialog box. Point category to the precompiled header. Select the entire project in the Tree View on the left ()

(Figure 1)

In the figure, we can see/FP "Debug/PCH" in the project options (the white place in the lower right corner. PCH ", which is generated by the specified. the name of the 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 (figure 2)

At this time, 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 function of/YC is to specify this file to create a PCH file. The file name after/YC is the header file that contains stable code. Only one file in a project can have the YC switch. VC compiles stdafx. cpp into an OBJ file and a PCH file based on this option.

Then, let's select another file,

Here, precomplier chooses use ......... Header file is the stdafx. 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. Note:

1): If/Yu is used, that is, pre-compilation is used. the beginning of a CPP file. I emphasize that it is the beginning of a file, including the file that you specified to generate a PCH file. h file (default is stdafx. h) otherwise there will be problems. If you do not include this file, you will be notified of unexpected file end. If you do not include it at the beginning, you can try the following and you will find it amazing .....

2) If you accidentally lose the PCH file, you only need to let the compiler generate a PCH File Based on the above analysis. That is to say, you can re-compile stdafx. cpp (that is, the CPP file of the specified/YC. Of course, you can rebuild all. Simply select the CPP file and press Ctrl + F7.

Method 2. Automatic use

You only need to specify/Yx. Or Select automatic ......... You can. Note that if you specify/YC/YU,/Yx will be ignored. The former has a higher priority.

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.