Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 558
user2543623 Avatar asked Jan 18 '17 02:01

user2543623


1 Answers

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.

like image 93
Ari0nhh Avatar answered Sep 17 '22 08:09

Ari0nhh