Why do I need to include the precompiled header in

2019-06-06 11:20发布

问题:

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?

回答1:

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.



回答2:

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!