INFO: AppWizard and Precompiled Header Files (119329)



The information in this article applies to:

  • The AppWizard, when used with:
    • Microsoft Visual C++ for Windows, 16-bit edition 1.0
    • Microsoft Visual C++ for Windows, 16-bit edition 1.5
    • Microsoft Visual C++ for Windows, 16-bit edition 1.51
    • Microsoft Visual C++ for Windows, 16-bit edition 1.52
    • Microsoft Visual C++, 32-bit Editions 1.0
    • Microsoft Visual C++, 32-bit Editions 2.0
    • Microsoft Visual C++, 32-bit Editions 2.1
    • Microsoft Visual C++, 32-bit Editions 4.0
    • Microsoft Visual C++, 32-bit Editions 5.0
    • Microsoft Visual C++, 32-bit Editions 6.0

This article was previously published under Q119329

SUMMARY

When you use AppWizard to generate a starter application, it sets up your project to use a precompiled header (.PCH) file. Precompiled headers help to speed up compilation when a group of files in a project do not change. In the case of Microsoft Foundation Class (MFC) applications generated by AppWizard, the group of files that do not change are the standard MFC header files.

AppWizard puts the line "#include 'stdafx.h'" as one of the first lines in every .CPP file in a project. Because of the compiler options being used by projects generated with AppWizard, anything up to and including "stdafx.h" in a .CPP file is considered by the compiler to be part of a precompiled header.

The problem with this is that when you edit the .CPP files and insert #includes, #defines, declarations, or other code before the "#include 'stdafx.h'" line, all of it is ignored by the compiler. This is the correct behavior because when the compiler is using the precompiled header, it starts to compile the code in the .CPP file after skipping past the "stdafx.h" line.

This behavior can lead to compiler errors that do not seem to make sense, because it appears that you are including all the needed declarations and header files in the .CPP file. For example, some of the errors that may result are the following:

  • error C2065: 'CTestClass' : undeclared identifier
  • error C2146: syntax error : missing ';' before identifier 'tc'
  • error C2064: term does not evaluate to a function
Even if the code compiles without errors, it may not run the way you expect to if there is code meant to be conditionally included or excluded based on whether a preprocessor macro was defined. This can occur if the line with the #define was inserted before the "stdafx.h" line. To avoid unexpected compiler errors or unexpected behavior in your application, make sure any code you add to an AppWizard-generated .CPP file comes after the "stdafx.h" line.

REFERENCES

"Visual C++ for Windows Programming Techniques" manual, chapter 1, "Using Precompiled Headers."

"Visual C++ for Windows NT Programming Techniques" manual, chapter 10, "Using Precompiled Headers."

Visual C++ Books Online topic "Using Precompiled Headers" in the "Visual C++ User's Guide" chapter on "Working With Projects."

Modification Type:MajorLast Reviewed:12/2/2003
Keywords:kbinfo kbwizard KB119329