Understanding Precompiled Headers
When the MFC Application Wizard generates a project, it generates switch settings and files for precompiled headers. You must understand how the make system processes precompiled headers in order to manage your projects effectively.
Note | Visual C++ .NET has two precompiled header "systems": automatic and manual. Automatic precompiled headers, which are activated by the /Yx compiler switch, store compiler output in a "database" file. Manual precompiled headers are activated by the /Yc and /Yu switch settings and are central to all the MFC Application Wizard–generated projects. |
Precompiled headers represent compiler "snapshots" taken at a particular line of source code. In MFC library programs, the snapshot is generally taken immediately after the following statement:
#include "StdAfx.h"
The file StdAfx.h contains #include statements for the MFC library header files. The file's contents depend on the options you select when you run the MFC Application Wizard, but the file always contain these statements:
#include <afxwin.h>
#include <afxext.h>
If you're using compound documents, StdAfx.h also contains this statement:
#include <afxole.h>
And if you're using Automation or ActiveX controls, it contains:
#include <afxdisp.h>
If you're using Internet Explorer 4.0 Common Controls, StdAfx.h contains this statement:
#include <afxdtctl.h>
Occasionally, you'll need other header files—for example, the header for template-based collection classes that is accessed by this statement:
#include <afxtempl.h>
The source file StdAfx.cpp contains only this statement:
#include "StdAfx.h"
This statement is used to generate the precompiled header file in the project directory. The MFC library headers included by StdAfx.h never change, but they do take a long time to compile. The compiler switch /Yc, used only with StdAfx.cpp , causes the creation of the precompiled header (PCH) file. The switch /Yu, used with all the other source code files, causes the use of an existing PCH file. The switch /Fp specifies the PCH filename that would otherwise default to the project name (with the PCH extension) in the target's output files subdirectory. Figure 3-1 illustrates the whole process.
Figure 3-1: The Visual C++ .NET precompiled header process.
The MFC Application Wizard sets the /Yc and /Yu switches for you, but you can make changes if you need to. It's possible to define compiler switch settings for individual source files. If you select only StdAfx.cpp in the C/C++ folder in the project's Property Pages dialog box, you'll see the /Yc setting on the Precompiled Headers property page. This overrides the /Yu setting that is defined for the target.Be aware that PCH files are big—10 MB is typical. If you're not careful, you'll fill up your hard disk. You can keep things under control by periodically cleaning out your projects' Debug directories, or you can use the /Fp compiler option to reroute PCH files to a common directory.