0

I have MyClass.cpp and MyClass.h files. Since MyClass.cpp has #include "MyClass.h" I put stdafx.h to MyClass.h. During compile I got error:

unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?

Why? If MyClass.cpp includes MyClass.h it should include all lines of MyClass.h and these lines contain stdafx.h.

Roman R.
  • 68,205
  • 6
  • 94
  • 158
vico
  • 17,051
  • 45
  • 159
  • 315
  • The command line to the compiler will include `/Yu"stdafx.h"`, so then "stdafx.h" must be the first header found, otherwise it throws error `C1010` – josh poley Aug 22 '14 at 16:14
  • 1
    See: [What's the use for “stdafx.h” in Visual Studio?](http://stackoverflow.com/a/4726838/868014) – Roman R. Aug 22 '14 at 16:23

2 Answers2

1

It doesn't matter if you put #include "stdafx.h" inside your .h file. The compiler doesn't care. It wants to see #include "stdafx.h" as the first line of your .cpp file, unless you have "Not using precompiled headers" as a compiler option for your source file.. The reasoning has to do with precompiled headers. I cannot really offer a technical explanation, but simply doing it that way will make life easier.

Logicrat
  • 4,438
  • 16
  • 22
1

It is the other way around if you want MyClass.h to be precompiled.

  • MyClass.h doesn't include stdafx.h
  • stdafx.h include MyClass.h
  • MyClass.cpp include stdafx.h as the first directive. For code clarity, it may also include MyClass.h.
UmNyobe
  • 22,539
  • 9
  • 61
  • 90