Share via


/YX   (Automatic Use of Precompiled Headers)

OverviewHow Do ICompiler Options

This option instructs the compiler to use a precompiled header file (PCH) if one exists or to create one if none exists. The file is created in the current directory. You can use the /Fp option to change the default name and location of the precompiled header. (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.)

Command Line Project Settings Description
/YX Automatic Use of Precompiled Headers
  • Creates a file named project.PCH if it doesn’t already exist, and compiles only header (.H) files into this .PCH file.

  • If you have no project open, it creates a file named VC50.PCH.

  • The inclusion of .H files stops when the compiler encounters the first declaration, definition, hdrstop pragma, or #line directive in the source file being compiled with the option, or after the .H file specified in the Through Header text box.

  • In subsequent compilations, the PCH is used after the compiler makes its final consistency check.

  • See Consistency Rules for /YX.
/YXfilename Through Header
  • When creating a PCH, the compiler compiles all code up to and including the .H file specified in the Through Header text box (filename).

  • When using a PCH, the compiler treats all code occurring before the specified .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.

The header files precompiled by /YX are determined by the compiler, which may elect to use a subset of header files available to improve the number of cases where the resulting precompiled header can be used.

Although it is usually best to let the compiler determine which header files to use, you can selectively precompile header files by placing a hdrstop pragma in the source file between two #include directives. All header files before the hdrstop pragma are precompiled; those after are not. When used with /YX, any filename specified with the hdrstop pragma is ignored. If any subsequent compilation using the precompiled header does not contain an identical hdrstop pragma at the same point in the source file, the compiler builds a new precompiled header.

Use of the /YX option implies use of the /Yd option (duplicate debugging information in all object files).

Example

The following command line uses /YX to create or use a precompiled header named VC50.PCH:

CL /YX MYPROG.CPP

The following command line creates a precompiled header named MYPROG.PCH and places it in the \PROJPCH directory:

CL /YX /Fp\PROJPCH\MYPROG.PCH MYPROG.CPP