2

If I don't include the stdafx even in an empty .cpp, I get this error

fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "StdAfx.h"' to your source?

Why do I need to include it even in dummy files?

sashoalm
  • 75,001
  • 122
  • 434
  • 781
unj2
  • 52,135
  • 87
  • 247
  • 375
  • 1
    You don't, you can disable precompiled headers. You can even do that on a per-file basis. –  Jun 15 '12 at 14:06
  • Precompiled headers is just a feature of the compiler to speed up compilation when you have lots of or big header files. It can, as @Fanael says, be disabled. – Some programmer dude Jun 15 '12 at 14:08

2 Answers2

6

You can disable precompiled headers per translation unit (in the properties for a given CPP file). You can probably do it per project too, if you're willing to explore the configuration GUI. Not all project types specify a PCH either; its just the standard Microsoft Way.

I've never encountered them outside of Microsoft land, so "in C++" is a little over general!

Rook
  • 5,734
  • 3
  • 34
  • 43
  • 1
    Other compilers like GCC also have a scheme for precompiled headers, but every implementation does it their own way. E.g. `g++ -o my_projects_pch.h.gch -x c++-header my_projects_pch.h` then `g++ -include my_projects_pch.h my_project.cpp` using -include will use a precompiled header if it exists. – bames53 Jun 15 '12 at 14:25
  • Indeed, but happily they are a slightly eccentric option and as such I've never seen em used. Maybe I'm just lucky, though. – Rook Jun 15 '12 at 14:32
5

If you use them, then you must include them. But you can turn them off in the project properties.

However, the recommended way to use them is to "Force Include" the PCH from the command line, so that the file itself doesn't contain the PCH. That way the source file can be compatible with other systems.

user541686
  • 205,094
  • 128
  • 528
  • 886
  • But why do I need to include them in ALL the files? It doesn't make sense at all. – unj2 Jun 15 '12 at 14:21
  • @kunj2aan: because PCH is enabled for *all* the files. :-) You can actually disable them individually if you go to the Properties of a file and say Don't use PCH. But by default, the project settings apply to *all* the files, so that's why. – user541686 Jun 15 '12 at 14:35
  • 1
    @RaymondChen: Yup, exactly. I think the problem (which also took me a while to understand when I had this question myself) is that PCH is not really a project property, but a source file property... but it's not *at all* obvious from the way it's set up, and it all looks like "magic" until you understand that `stdafx.cpp` is doing the real work, not `stdafx.h`. – user541686 Jun 15 '12 at 14:38
  • Ah ok that makes a little more sense.. @Mhrdad : What is this magic in the stdafx.cpp : ) ? – unj2 Jun 15 '12 at 16:59
  • @kunj2aan: It's the "Create precompiled headers" option when you go to the properties of stdafx.cpp, which is different from all other files'. I never knew it existed until I read it somewhere. – user541686 Jun 15 '12 at 17:32