About pre-compiled Headers

Source: Internet
Author: User

1. What is a pre-compiled header?

The pre-compiled header is physically and normally used. OBJ files are the same, but are compiled into the pre-compilation header. H ,. C ,. the CPP file is compiled only once during the entire compilation process. If the part involved in the pre-compilation header is not changed, this part will not be re-compiled during subsequent compilation. This greatly improves the compilation speed and facilitates the management of the first-class files. It also helps prevent repeated inclusion issues.

Ii. When to use the pre-compiled header?

When most. C or. cpp files require the same header file.

When some code is repeatedly used.

When you import functions that are implemented in different databases and create confusion.

Iii. Compilation switch and usage involved in the pre-compilation header (for msvc)

1. Automatic pre-Compilation: the compiler determines when to create and use the pre-compilation header.

Compilation parameter:-Yx. Use this parameter to create a precompiled header file named vcxy. PCH. X and Y are the version numbers of VC. You can use-FP to specify the output path and file name of the PCH file.

For example, Cl-C-Yx-FP "precomp. PCH" ** will generate the precompiled header file precomp. PCH in the current directory. ** Indicates the source file.

2. Manual pre-Compilation:

Compilation parameters:

-YC "Name of the precompiled header file": by default, the precompiled header file with the same name as the header file is created. To use another name, use the-FP option.

For example, Cl-C-YC "precomp. h" ** will create a precompiled header file of precomp. PCH.

 

-Yu "Name of the pre-compiled header file" indicates that this pre-compiled header is used when a source file is not re-compiled. For example:

CL-C-Yu "precomp. h "**

Note: The pre-compiled headers must have been created before.

 

4. simple use of pre-compilation technology in makefile

 

OBJ/i386/debug. OBJ: Debug. c Debug. h

$ (CC)-YC "precomp. h"-fo $ @ Debug. c

OBJ/i386/operatereg. OBJ: operatereg. c operatereg. h

$ (CC)-Yu "precomp. h"-fo $ @ operatereg. c

OBJ/i386/watermark. OBJ: watermark. c watermark. h

$ (CC)-Yu "precomp. h"-fo $ @ watermark. c

The following is the same.

Precomp. h indicates the header file to be precompiled.

 

5. Some complicated methods for using pre-compiled Headers

For example:

cl -c -Yc"stuff.h" -Fplevel1.pch level1.cpp       -------------(1)
cl -c -Yu"stuff.h" -Fplevel1.pch -Yc level2.cpp   --------------(2)
Level2.cpp starts like this, where # pragma hdrstop indicates that the pre-compilation is complete, so the code can also be placed in the pre-compilation header.
#include "stuff.h"
#include "morestuff.h"
#pragma hdrstop("level2.pch")
 
(1) create a pre-compiled header named level1.pch using stuff. h.
(2) Based on the pre-compilation header level1.pch established from stuff. H, refer to level2.cpp to create another pre-compilation header level2.pch.
For more information about the naming rules, see how to use them when YC has no parameters.
 
For other usage rules of the pre-compiled header, see msdn.
 
Appendix:
1. Pre-compilation and guard macros
These two concepts have no relationship at all. If we need to find the same place, it means that the pre-compilation header also helps to control Repeated inclusion.
#ifndef _PLOTBRUSH_
#define _PLOTBRUSH_
---
#endif  // _PLOTBRUSH_
This guard macro prevents the same. h file from being contained twice in the same. c file.
Prevents
#include <Windows.h>
#include <Windows.h>
.
The pre-compilation header is equivalent to controlling the issues that are only compiled once from a global perspective.
The guard macro is only valid for the current compilation unit. The _ plotbrush _ defined in 1.C has no effect on 2.c.
 
2. When building in 98ddk, use build-Z to compile only the changed source files. -C clears the target folder.
Correct the error if any.
Written by Li Zhiyong
                                   2003/3/21

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.