8

For some reason I can't explain, the compiler is outputting an error saying that it found an unexpected #else token.

This occurs at the beginning of the file :

#if defined( _USING_MFC )
   #include "stdafx.h"
#else
   #include <windows.h>
#endif

There is nothing before that peice of code expect several (single-line) comments.

This error occurs in a .cpp file. What you see above is the beginning of the file. There is nothing before that.

I tried adding the following code before the code defined above, and the error is now an unexpected #endif

#if 1
   #include "stdafx.h"
#endif

So I suspect there is an issue with the included stdafx.h file which contains the following code :

#ifndef STDAFX_H_INCLUDED
#define STDAFX_H_INCLUDED

#include <Afx.h>
#include <Windows.h>

using namespace ATL;

#endif // STDAFX_H_INCLUDED

There's really nothing special about it. I'm also including this stdafx.h file from a stdafx.cpp file that only contains the #include statement, and it compiles correctly.

Here are the project preprocessor definitions :

_DEBUG
_WIN32_WCE=$(CEVER)
UNDER_CE
WINCE
DEBUG
_WINDOWS
$(ARCHFAM)
$(_ARCHFAM_)
_UNICODE
UNICODE
_TERMINAL_FALCONX3_CE6
_NO_CPP_EXCEPTIONS
_DONT_INCLUDE_WS_HEADERS
_USING_MFC

And some extra informations : Compiling for Windows CE 6 using Visual Studio 2008.

What would be causing this ? Thank you.

Virus721
  • 8,061
  • 12
  • 67
  • 123
  • 1
    Upvoted to counter downvote. People throw downvotes around too easily at questions, in my opinion yours is perfectly reasonable. – Smeeheey Jul 22 '16 at 08:52
  • It is not unreasonable, it just seems that it lacks some details. I don't think it is possible to figure out the problem with the provided information. But, I might be wrong so let me read the answer. – Iharob Al Asimi Jul 22 '16 at 09:00

1 Answers1

11

Based on the name stdafx, I assume it is a precompiled header.

A precompiler header must be the first include (preprocessor) directive in the file, you can't put anything (not even an ifdef) before it. The only exception being a few comment lines, as those would be ignore anyway.

Based on your example, you should put the #ifdef _USING_MFC into your stdafx.h, and include Afx.h there.

Dutow
  • 5,638
  • 1
  • 30
  • 40
  • 2
    Explanation what happens: Everything up to `#include "stdafx.h"` is replaced with by the contents of the precompiled `stdafx.pch`. This also replaces the `#if` but not the `#else`, so the first non-precompiled line is indeed `#else`. That's certainly unexpected. – MSalters Jul 22 '16 at 12:26