C++ stdafx.h:我什么时候需要它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/676385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
stdafx.h: When do I need it?
提问by Ashwin Nanjappa
I see so much code including stdafx.h. Say, I do notwant pre-compiled headers. And I willinclude all the required system headers myself manually. In that case is there any other good reason I should be aware of where I require stdafx.h
?
我看到了很多代码,包括 stdafx.h。说,我不想要预编译的头文件。我将自己手动包含所有必需的系统标头。在这种情况下,还有什么其他充分的理由我应该知道我需要的地方stdafx.h
吗?
采纳答案by 1800 INFORMATION
If you don't want to use precompiled headers, then there is no point to using a standard include file - this will slow down the build for every file that includes it and cause them to include extra stuff that they do not need. Get rid of it and just include the headers they need.
如果您不想使用预编译的头文件,那么使用标准包含文件是没有意义的——这会减慢包含它的每个文件的构建速度,并导致它们包含不需要的额外内容。摆脱它,只包含他们需要的标题。
回答by kgiannakakis
Even without pre-compiled headers stdafx.h could be handy as it groups header includes and definitions common for all files.
即使没有预编译的头文件 stdafx.h 也很方便,因为它对所有文件的头文件包含和定义进行了分组。
You can of course choose to repeat all these definitions in every file. stdafx.h is not strictly necessary.
您当然可以选择在每个文件中重复所有这些定义。stdafx.h 不是绝对必要的。
回答by sharptooth
stdafx.h is just another header file. If you fell you don't need it feel free to not include it and remove it from the project.
stdafx.h 只是另一个头文件。如果你摔倒了,你不需要它,可以随意不包含它并将其从项目中删除。
However it's quite typical to have a file like stdafx.h exactly for precompiled headers to work and to not include all the stuff manually in each source file.
然而,有一个像 stdafx.h 这样的文件完全是为了预编译头工作,而不是在每个源文件中手动包含所有内容,这是非常典型的。
回答by sharptooth
You can use pre-compiled headers (which are a good thing) without using stdafx.h (I abominate it too). I've only got access to VC++ 6.0 but in that go to Project Settings|C/C++|Precompiled Headers and select "automatic use of precompiled header" but leave the "compiled through" box empty.
您可以使用预编译的头文件(这是一件好事)而不使用 stdafx.h(我也讨厌它)。我只能访问 VC++ 6.0,但在其中转到 Project Settings|C/C++|Precompiled Headers 并选择“自动使用预编译头”,但将“编译通过”框留空。
回答by snemarch
As others have mentioned: if you don't need precompiled headers, you don't really need stdafx.h . And using it just to group common includes is pretty bad practice, actually.
正如其他人所提到的:如果您不需要预编译头文件,那么您实际上并不需要 stdafx.h 。实际上,仅使用它来对常见的包含进行分组是非常糟糕的做法。
In fact, even when using precompiled headers, it's good practice to include the headers your process actually needs after stdafx.h (or precompiled.h or whatever you want to call it) - along with #ifdef magic in your precompiled header to turn off the usage of PCH.
事实上,即使在使用预编译头文件时,最好在 stdafx.h(或 precompiled.h 或任何您想调用的文件)之后包含您的进程实际需要的头文件 - 以及预编译头文件中的 #ifdef 魔法以关闭PCH的使用。
Why? In order to check your module dependencies. Being able to disable your PCH lets you catch whether you're including necessary modules or not, and you can then write a tool to check your module interdependencies by parsing your .cpp and .h files (excluding the PCH header, of course).
为什么?为了检查您的模块依赖项。能够禁用 PCH 可以让您了解是否包含必要的模块,然后您可以编写一个工具来通过解析 .cpp 和 .h 文件(当然不包括 PCH 标头)来检查模块的相互依赖性。
回答by Gary Stewart
I know this is an old thread, but I thought I'd pass my opinion to readers. I found this in 2017, so I'm sure I'm not the only one that will be here.
我知道这是一个旧线程,但我想我会向读者传达我的意见。我是在 2017 年发现的,所以我确定我不是唯一会在这里的人。
There is an advantage to using pre-compiled headers, that may be overlooked by others because they have been using their practice for many years. The C++ standard has evolved greatly. Instead of including vector in 20 files that you might need and and or whatever, you use a PCH file and the compiler has to do less work, and that makes happy faces for everyone. Also you can put your common macros in there, such as VERIFY, and ASSERT and smart class objects. Don't put your class headers in there, it wouldn't make sense for that, rather the standard library or something you will need to use globally in a lot of places such as the macros I mentioned.
使用预编译头文件有一个优势,但其他人可能会忽略这一点,因为他们已经使用了多年的实践。C++ 标准已经有了很大的发展。不是在您可能需要的 20 个文件中包含 vector 和或其他文件,而是使用 PCH 文件,编译器必须做更少的工作,这让每个人都高兴。您也可以将常用的宏放在那里,例如 VERIFY、ASSERT 和智能类对象。不要把你的类头放在那里,它没有意义,而是标准库或你需要在很多地方全局使用的东西,比如我提到的宏。
basically realize this, by including the headers you need in every file you need, such as iostream, string, and vector, you are effectively compiling that every single time as an inline to the file. Include a "Pre Compiled" header, well just the name should ring a bell there.
基本上意识到这一点,通过在您需要的每个文件中包含您需要的头文件,例如 iostream、字符串和向量,您每次都有效地将其编译为文件的内联。包括一个“预编译”标题,那么名字应该在那里响起。