简体   繁体   English

为什么我需要在C ++的所有文件中包含预编译的头文件?

[英]Why do I need to include the precompiled header in all the files in C++?

If I don't include the stdafx even in an empty .cpp, I get this error 如果即使在空的.cpp中也不包含stdafx,则会出现此错误

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? 为什么即使在虚拟文件中也需要包含它?

You can disable precompiled headers per translation unit (in the properties for a given CPP file). 您可以禁用每个翻译单元的预编译头(在给定CPP文件的属性中)。 You can probably do it per project too, if you're willing to explore the configuration GUI. 如果您愿意探索配置GUI,则可能也可以针对每个项目进行操作。 Not all project types specify a PCH either; 并非所有的项目类型都指定PCH。 its just the standard Microsoft Way. 它只是标准的Microsoft Way。

I've never encountered them outside of Microsoft land, so "in C++" is a little over general! 我从未在Microsoft领域之外遇到过它们,因此“在C ++中”有点笼统!

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. 但是, 建议使用它们的方法是从命令行 “强制包含” PCH,以便文件本身不包含PCH。 That way the source file can be compatible with other systems. 这样,源文件可以与其他系统兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM