Share via


/Yu   (Use Precompiled Header File)

OverviewHow Do ICompiler Options

This option instructs the compiler to use an existing precompiled header in the current compilation.

Command Line Project Settings Description
/Yu Use Precompiled Header File This option specifies using a precompiled header (.PCH) file during builds. The PCH must have been created using Create Precompiled Header File (/Yc).
/Yufilename Through Header Type the name of a header (.H) file in the Through Header text box. The compiler treats all code occurring before the .H file as precompiled. It skips to just beyond the #include directive associated with the .H file, uses the code contained in the .PCH file, and then compiles all code after filename.

On the command line, no space is allowed between /Yu and filename.

To find this option in the development environment, click Settings on the Project menu. Then click the C/C++ tab, and click Precompiled Headers in the Category box.

When you specify the /Yu option without a file name, your source program must contain #pragma hdrstop pragma that specifies the file name. The compiler skips to the location of that pragma, restores the compiled state from the precompiled header file specified by the pragma, and then compiles only code that follows the pragma. If #pragma hdrstop does not specify a file name, the compiler looks for a file with a name derived from the base name of the source file with a .PCH extension.

If you specify the /Yu option without a file name and fail to specify a hdrstop pragma, an error message is generated and the compilation is unsuccessful.

Note   If the options /Ycfilename and /Yufilename occur on the same command line and both reference, or imply, the same file name, /Ycfilename, takes precedence. This feature simplifies the writing of makefiles.

Examples

If the following code:

#include <afxwin.h>   // Include header for class library
#include "resource.h" // Include resource definitions
#include "myapp.h"    // Include information specific to this app
...

is compiled with the command line:

CL /YuMYAPP.H PROG.CPP

the compiler does not process the three include statements but uses precompiled code from MYAPP.PCH, thereby saving the time involved in preprocessing all three of the files (and any files they might include).

You can use the /Fp option with the /Yu option to specify the name of the .PCH file if the name is different from either the file name argument to /Yc or the base name of the source file, as in the following:

CL /YuMYAPP.H /FpMYPCH.PCH PROG.CPP

This command specifies a precompiled header file named MYPCH.PCH. The compiler uses its contents to restore the precompiled state of all header files up to and including MYAPP.H. The compiler then compiles the code that occurs after the MYAPP.H include statement.