Freigeben über


Two Choices for Precompiling Code 

With Visual C++, 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 common sets of header files but don't include them in the same order, or when you want to 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 create a precompiled header. When used with the optional hdrstop pragma, /Yc lets you precompile both header files and source code. Select /Yu to use an existing precompiled header in the existing compilation. You can also use /Fp with the /Yc and /Yu options to provide an alternative name for the precompiled header.

The compiler option reference topics for /Yu and /Yc discuss how to access this functionality in the development environment.

Building a PCH File

You can use one PCH to build another in a fairly simple way.

cl -c -Yc"stuff.h" -Fplevel1.pch level1.cpp
cl -c -Yu"stuff.h" -Fplevel1.pch -Yc level2.cpp

The level2.cpp file looks like this:

#include "stuff.h"
#include "morestuff.h"
#pragma hdrstop("level2.pch")

The following command will build level2.pch, which can be used in further compiles:

cl -c -Yu"morestuff.h" -Fplevel2.pch mysource.cpp

Note that you only need to put the #pragma hdrstop directive in the file that builds the second PCH; you don't have to put it in all .cpp files that use the PCH. Files that use the PCH can name the morestuff.h file in the command line (so you don't have to edit all your files to use this). Either method of precompiling code — automatically or manually — stores the resulting precompiled code in a precompiled header.

More Information

For further examples using precompiled headers, see the makefiles used to build the sample programs that ship with the Microsoft Foundation Class Library.

See Also

Reference

Creating Precompiled Header Files