Last Modification: December 11, 1999

Why is Visual C++ ignoring my header file?
I'm getting a "Fatal error C1020: unexpected #endif" but I don't understand why. My #ifdef and and #endif statements match just fine.


Chances are your code looks something like this:
Example 1:
#ifdef DEFINED
#include "stdafx.h"
#endif

Example 2:
#include "myheader.h"
#include "stdafx.h"

And you are compiling with the /Yu"stdafx.h" compiler option.

New projects in Visual C++ set this option by default to increase compilation speed. If you want to change the option, use the Project.Settings.C++.Precompiled Headers dialog.

The help topic "The /Yu (Use Precompiled Header) Option" from the Creating Precompiled Header Files part of the Visual C++ Programmer's Guide explains the behavior you see:

"For /Yu, the compiler assumes that all code occurring before filename is precompiled. The compiler skips to the specified #include directive, uses the code contained in the precompiled header file, and then compiles all code after filename."

That is, everything in your code file before the #include "stdafx.h" line is completely ignored, because the compiler assumes that the necessary information is in your precompiled header.

You can fix problems caused by this behavior several ways:
* For example 1, move the #include "stdafx.h" outside of the #ifdef
* For example 2, move the #include "myheader.h" into the stdafx.h file
* For example 2, move the #include "myheader.h" after the stdafx.h file
* For either example, turn off precompiled headers option for the module that's causing problems. (This will make compilation of that module take longer.)