Stdafx.h là gì

When you create a new project in Visual Studio, a precompiled header file named pch.h is added to the project. (In Visual Studio 2017 and earlier, the tệp tin was called stdafx.h.) The purpose of the tệp tin is khổng lồ speed up the build process. Any stable header files, for example Standard Library headers such as , should be included here. The precompiled header is compiled only when it, or any files it includes, are modified. If you only make changes in your project source code, the build will skip compilation for the precompiled header.

Bạn đang xem: Stdafx.h là gì

The compiler options for precompiled headers are /Y. In the project property pages, the options are located under Configuration Properties > C/C++ > Precompiled Headers. You can choose lớn not use precompiled headers, and you can specify the header tệp tin name và the name & path of the output file.

Custom precompiled code

For large projects that take significant time lớn build, you may want lớn consider creating custom precompiled files. The savoirjoaillerie.com C và C++ compilers provide options for precompiling any C or C++ code, including inline code. Using this performance feature, you can compile a stable body of code, store the compiled state of the code in a file, &, during subsequent compilations, combine the precompiled code with code that is still under development. Each subsequent compilation is faster because the stable code does not need khổng lồ be recompiled.

When khổng lồ Precompile Source Code

Precompiled code is useful during the development cycle khổng lồ reduce compilation time, especially if:

You always use a large body toàn thân of code that changes infrequently.

Your program comprises multiple modules, all of which use a standard phối of include files and the same compilation options. In this case, all include files can be precompiled inkhổng lồ one precompiled header.

The first compilation — the one that creates the precompiled header (PCH) file — takes a bit longer than subsequent compilations. Subsequent compilations can proceed more quickly by including the precompiled code.

You can precompile both C & C++ programs. In C++ programming, it is comtháng practice to separate class interface information into header files. These header files can later be included in programs that use the class. By precompiling these headers, you can reduce the time a program takes khổng lồ compile.


Note

Although you can use only one precompiled header (.pch) tệp tin per source tệp tin, you can use multiple .pch files in a project.


Two Choices for Precompiling Code

You can precompile any C or C++ code; you are not limited to precompiling only header files.

Precompiling requires planning, but it offers significantly faster compilations if you precompile source code other than simple header files.

Precompile code when you know that your source files use comtháng sets of header files but don"t include them in the same order, or when you want lớn include source code in your precompilation.

The precompiled-header options are /Yc (Create Precompiled Header File) and /Yu (Use Precompiled Header File). Use /Yc to lớn create a precompiled header. When used with the optional hdrstop pragma, /Yc lets you precompile both header files and source code. Select /Yu lớn use an existing precompiled header in the existing compilation. You can also use /Fp with the /Yc/Yu options to provide an alternative sầu name for the precompiled header.

The compiler option reference topics for /Yu and /Yc discuss how khổng lồ access this functionality in the development environment.

Precompiled Header Consistency Rules

Because PCH files contain information about the machine environment as well as memory address information about the program, you should only use a PCH file on the machine where it was created.

Consistency Rules for Per-File Use of Precompiled Headers

The /Yu compiler option lets you specify which PCH tệp tin lớn use.

When you use a PCH tệp tin, the compiler assumes the same compilation environment — one that uses consistent compiler options, pragmas, & so on — that was in effect when you created the PCH file, unless you specify otherwise. If the compiler detects an inconsistency, it issues a warning và identifies the inconsistency where possible. Such warnings do not necessarily indicate a problem with the PCH file; they simply warn you of possible conflicts. Consistency requirements for PCH files are described in the following sections.

Compiler Option Consistency

The following compiler options can trigger an inconsistency warning when using a PCH file:

Macros created using the Preprocessor (/D) option must be the same between the compilation that created the PCH file và the current compilation. The state of defined constants is not checked, but unpredictable results can occur if these change.

PCH files do not work with the /E & /EP options.

PCH files must be created using either the Generate Browse Info (/FR) option or the Exclude Local Variables (/Fr) option before subsequent compilations that use the PCH tệp tin can use these options.

C 7.0-Compatible (/Z7)

If this option is in effect when the PCH tệp tin is created, subsequent compilations that use the PCH tệp tin can use the debugging information.

If the C 7.0-Compatible (/Z7) option is not in effect when the PCH tệp tin is created, subsequent compilations that use the PCH file & /Z7 trigger a warning. The debugging information is placed in the current .obj file, và local symbols defined in the PCH tệp tin are not available to the debugger.

Include Path Consistency

A PCH file does not contain information about the include path that was in effect when it was created. When you use a PCH file, the compiler always uses the include path specified in the current compilation.

Xem thêm: Hướng Dẫn Cách Bỏ Nền Màu Trong Word Đơn Giản Nhất!, Cách Xóa Màu Nền Văn Bản Word

Source File Consistency

When you specify the Use Precompiled Header File (/Yu) option, the compiler ignores all preprocessor directives (including pragmas) that appear in the source code that will be precompiled. The compilation specified by such preprocessor directives must be the same as the compilation used for the Create Precompiled Header File (/Yc) option.

Pragma Consistency

Pragmas processed during the creation of a PCH tệp tin usually affect the file with which the PCH file is subsequently used. The comment and message pragmas bởi vì not affect the remainder of the compilation.

These pragmas affect only the code within the PCH file; they bởi vì not affect code that subsequently uses the PCH file:


Consistency Rules for /Yc và /Yu

When you use a precompiled header created using /Yc or /Yu, the compiler compares the current compilation environment to the one that existed when you created the PCH tệp tin. Be sure lớn specify an environment consistent with the previous one (using consistent compiler options, pragmas, & so on) for the current compilation. If the compiler detects an inconsistency, it issues a warning and identifies the inconsistency where possible. Such warnings don"t necessarily indicate a problem with the PCH file; they simply warn you of possible conflicts. The following sections explain the consistency requirements for precompiled headers.

Compiler Option Consistency

This table lists compiler options that might trigger an inconsistency warning when using a precompiled header:

OptionNameRule
/DDefine constants and macrosMust be the same between the compilation that created the precompiled header và the current compilation. The state of defined constants is not checked, but unpredictable results can occur if your files depkết thúc on the values of the changed constants.
/E or /EPCopy preprocessor output lớn standard outputPrecompiled headers bởi vì not work with the /E or /EP option.
/Fr or /FRGenerate savoirjoaillerie.com Source Browser informationFor the /Fr & /FR options khổng lồ be valid with the /Yu option, they must also have been in effect when the precompiled header was created. Subsequent compilations that use the precompiled header also generate Source Browser information. Browser information is placed in a single .sbr tệp tin và is referenced by other files in the same manner as CodeView information. You cannot override the placement of Source Browser information.
/GA, /GD, /GE, /Gw, or /GWWindows protocol optionsMust be the same between the compilation that created the precompiled header và the current compilation. If these options differ, a warning message results.
/ZiGenerate complete debugging informationIf this option is in effect when the precompiled header is created, subsequent compilations that use the precompilation can use that debugging information. If /Zi is not in effect when the precompiled header is created, subsequent compilations that use the precompilation và the /Zi option trigger a warning. The debugging information is placed in the current object file, và local symbols defined in the precompiled header are not available to the debugger.

Using Precompiled Headers in a Project

Previous sections present an overview of precompiled headers: /Yc and /Yu, the /Fp option, and the hdrstop pragma. This section describes a method for using the manual precompiled-header options in a project; it ends with an example maketệp tin & the code that it manages.

For another approach to using the manual precompiled-header options in a project, study one of the makefiles located in the MFCSRC directory that is created during the default setup of Visual Studio. These makefiles take a similar approach lớn the one presented in this section but make greater use of savoirjoaillerie.com Program Maintenance Utility (NMAKE) macros, and offer greater control of the build process.

PCH Files in the Build Process

The code base of a software project is usually contained in multiple C or C++ source files, object files, libraries, & header files. Typically, a makefile coordinates the combination of these elements inlớn an executable file. The following figure shows the structure of a makefile that uses a precompiled header tệp tin. The NMAKE macro names & the file names in this diagram are consistent with those in the example code found in Sample Maketệp tin for PCH and Example Code for PCH.

The figure uses three diagrammatic devices lớn show the flow of the build process. Named rectangles represent each tệp tin or macro; the three macros represent one or more files. Shaded areas represent each compile or liên kết action. Arrows show which files and macros are combined during the compilation or linking process.

*
Structure of a Makefile That Uses a Precompiled Header File

Beginning at the top of the diagram, both STABLEHDRS & BOUNDRY are NMAKE macros in which you danh mục files not likely to need recompilation. These files are compiled by the command string

CL /c /W3 /Yc$(BOUNDRY) applib.cpp myứng dụng.cpp

only if the precompiled header file (STABLE.pch) does not exist or if you make changes lớn the files listed in the two macros. In either case, the precompiled header tệp tin will contain code only from the files listed in the STABLEHDRS macro. List the last file you want precompiled in the BOUNDRY macro.

The files you danh sách in these macros can be either header files or C or C++ source files. (A single PCH tệp tin cannot be used with both C và C++ modules.) Note that you can use the hdrstop macro to lớn stop precompilation at some point within the BOUNDRY tệp tin. See hdrstop for more information.

Continuing down the diagram, APPLIB.obj represents the tư vấn code used in your final application. It is created from APPLIB.cpp, the files listed in the UNSTABLEHDRS macro, & precompiled code from the precompiled header.

MYAPP..obj represents your final application. It is created from MYAPP..cpp, the files listed in the UNSTABLEHDRS macro, and precompiled code from the precompiled header.

Finally, the executable file (MYAPPhường.EXE) is created by linking the files listed in the OBJS macro (APPLIB.obj and MYAPPhường.obj).

Sample Maketệp tin for PCH

The following makefile uses macros & an !IF, !ELSE, !ENDIF flow-of-control commvà structure lớn simplify its adaptation khổng lồ your project.

# Maketệp tin : Illustrates the effective sầu use of precompiled# headers in a project# Usage: NMAKE option# option: DEBUG=<0|1># (DEBUG not defined is equivalent to lớn DEBUG=0)#OBJS = myphầm mềm.obj applib.obj# List all stable header files in the STABLEHDRS macro.STABLEHDRS = stable.h another.h# List the final header tệp tin to be precompiled here:BOUNDRY = stable.h# List header files under development here:UNSTABLEHDRS = unstable.h# List all compiler options comtháng khổng lồ both debug and final# versions of your code here:CLFLAGS = /c /W3# List all linker options common to both debug & final# versions of your code here:LINKFLAGS = /nologo!IF "$(DEBUG)" == "1"CLFLAGS = /D_DEBUG $(CLFLAGS) /Od /ZiLINKFLAGS = $(LINKFLAGS) /CODLIBS = slibce!ELSECLFLAGS = $(CLFLAGS) /Oselg /GsLINKFLAGS = $(LINKFLAGS)LIBS = slibce!ENDIFmyapp.exe: $(OBJS) links $(LINKFLAGS)
Aside from the STABLEHDRS, BOUNDRY, and UNSTABLEHDRS macros shown in the figure "Structure of a Makefile That Uses a Precompiled Header File" in PCH Files in the Build Process, this maketệp tin provides a CLFLAGS macro and a LINKFLAGS macro. You must use these macros to list compiler & linker options that apply whether you build a debug or final version of the application"s executable file. There is also a LIBS macro where you danh sách the libraries your project requires.

The maketệp tin also uses !IF, !ELSE, !ENDIF lớn detect whether you define a DEBUG symbol on the NMAKE commvà line:

NMAKE DEBUG=<1|0>This feature makes it possible for you khổng lồ use the same maketệp tin during development và for the final versions of your program — use DEBUG=0 for the final versions. The following commvà lines are equivalent:

NMAKENMAKE DEBUG=0For more information on makefiles, see NMAKE Reference. Also see MSVC Compiler Options và the MSVC Linker Options.

Xem thêm: Fine Hair Là Gì ? Hair Treatments Là Gì

Example Code for PCH

The following source files are used in the maketệp tin described in PCH Files in the Build Process & Sample Makefile for PCH. cảnh báo that the comments contain important information.

// ANOTHER.H : Contains the interface lớn code that is not// likely lớn change.//#ifndef __ANOTHER_H#define __ANOTHER_H#includevoid savemoretime( void );#endif // __ANOTHER_H// STABLE.H : Contains the interface lớn code that is not likely// khổng lồ change. List code that is likely khổng lồ change// in the makefile"s STABLEHDRS macro.//#ifndef __STABLE_H#define __STABLE_H#includevoid savetime( void );#endif // __STABLE_H// UNSTABLE.H : Contains the interface khổng lồ code that is// likely lớn change. As the code in a header// tệp tin becomes stable, remove the header file// from the makefile"s UNSTABLEHDR macro và list// it in the STABLEHDRS macro.//#ifndef __UNSTABLE_H#define __UNSTABLE_H#includevoid notstable( void );#endif // __UNSTABLE_H// APPLIB.CPPhường. : This file contains the code that implements// the interface code declared in the header// files STABLE.H, ANOTHER.H, và UNSTABLE.H.//#include"another.h"#include"stable.h"#include"unstable.h"using namespace std;// The following code represents code that is deemed stable and// not likely khổng lồ change. The associated interface code is// precompiled. In this example, the header files STABLE.H and// ANOTHER.H are precompiled.void savetime( void ) { cout // MYAPP.CPP : Sample application// All precompiled code other than the file listed// in the makefile"s BOUNDRY macro (stable.h in// this example) must be included before the file// listed in the BOUNDRY macro. Unstable code must// be included after the precompiled code.//#include"another.h"#include"stable.h"#include"unstable.h"int main( void ) savetime(); savemoretime(); notstable();

See also

C/C++ Building ReferenceMSVC Compiler Options


Chuyên mục: Blogs