Why does Visual Studio C++ require including "StdAfx.h" even on files that don't need it?

user2543623 picture user2543623 · Jan 18, 2017 · Viewed 7.8k times · Source

I understand what precompiled headers are doing with "#include "StdAfx.h" and yes, I know I can turn them off. But that's not my question.

If you're using precompiled headers, Visual C++ requires every cpp file to #include "StdAfx.h", even the files that aren't using any of the headers in StdAfx.h. If you forget to include StdAfx.h on one file, it's an error. But why? The obvious approach would be just "If you include StdAfx.h then that file will use it, but if you forget to include it, then those header files will simply not be included." I don't understand why VC++ would require you to include StdAfx.h when it's not needed. Seems like it would have been easier for them to treat it like a normal header file.

Is there any good reason why this is required?

Answer

Ari0nhh picture Ari0nhh · Jan 18, 2017

Just a addition to the Marks answer. In fact, you do not have to manually include stdafx.h in the all project source files. You may use project option Forced Include Files: enter image description here

That way stdafx.h will be automatically included in all your sources.