Microsoft KB Archive/119329

= INFO: AppWizard and Precompiled Header Files =

Article ID: 119329

Article Last Modified on 12/2/2003

-

APPLIES TO

 The AppWizard, when used with:  Microsoft Visual C++ 1.0 Professional Edition

 Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.51

 Microsoft Visual C++ 1.52 Professional Edition</li></ul>

 Microsoft Visual C++ 1.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.1</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 5.0 Standard Edition</li></ul>

 Microsoft Visual C++ 6.0 Service Pack 5</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q119329

<div class="summary_section">

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.

<div class="references_section">