The /Yu (Use Precompiled Header) Option

Home

The /Yu (Use Precompiled Header) option instructs the compiler to use an existing precompiled header in the existing compilation. The syntax of this option is:

/Yufilename

Using /Yu with a Filename

The filename argument is the name of a header file, which is included in the source file using an #include preprocessor directive. The name of the include file must be the same for both the /Yc option that creates the precompiled header and any subsequent /Yu option indicating use of the precompiled header.

For /Yc, filename specifies the point at which precompilation stops; the compiler precompiles all code though filename and names the resulting precompiled header using the base name of the include file and an extension of .PCH. For /Yu, the compiler assumes that all code occurring before filename is precompiled. The compiler skips to the specified #include directive, uses the code contained in the precompiled header file, and then compiles all code after filename.

Consider 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
                      //   application
...

When compiled with the command line

CL /YuMYAPP.H PROG.CPP

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

Using /Yu Without a Filename

When you specify the /Yu option without a filename, your source program must contain #pragma hdrstop. The compiler skips to the location of that pragma and uses the content of the precompiled header file specified by the pragma. If the hdrstop pragma does not specify a filename, the name is derived from the base name of the source file with the .PCH extension. You can also use the /Fp option to specify a different .PCH file.

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

Note   If the /Ycfilename and /Yufilename options occur on the same command line and both reference the same filename, /Ycfilename takes precedence, precompiling all code up to and including the named file. This feature simplifies the writing of makefiles.