KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q119329: INFO: AppWizard and Precompiled Header Files

Article: Q119329
Product(s): Microsoft C Compiler
Version(s): 1.0,1.5,1.51,1.52,2.0,2.1,4.0,5.0,6.0
Operating System(s): 
Keyword(s): kbwizard kbVC100 kbVC150 kbVC151 kbVC152 kbVC200 kbVC210 kbVC400 kbVC500 kbVC600 kbGrpD
Last Modified: 11-FEB-2002

-------------------------------------------------------------------------------
The information in this article applies to:

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

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."

Additional query words: random incorrect

======================================================================
Keywords          : kbwizard kbVC100 kbVC150 kbVC151 kbVC152 kbVC200 kbVC210 kbVC400 kbVC500 kbVC600 kbGrpDSTools 
Technology        : kbVCsearch kbAudDeveloper kbAppWizard
Version           : :1.0,1.5,1.51,1.52,2.0,2.1,4.0,5.0,6.0
Issue type        : kbinfo

=============================================================================

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.