MSVC 预编译头:哪些文件需要#include“stdafx.h”?
使用预编译头时,是否每个文件都需要#include "stdafx.h"
? 或者只需要源文件包含它。
编辑:另外,我的预编译头文件#includes
有很多STL头文件。 但是,在我的标头中,有时我有返回 std::vector
或类似内容的函数,所以无论如何我都需要 #include
。 这比包含 stdafx.h 更糟糕吗? 我需要包含我的单元测试框架的定义。
Does every file need to #include "stdafx.h"
when using precompiled headers? Or do only source files need to include it.
EDIT: Also, my precompiled header file #includes
a lot of STL headers. But, in my headers, I sometimes have functions that return std::vector
or something like that, so I need to #include <vector>
anyway. Is this worse than including stdafx.h? I need to include the definitions for my unit testing framework.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
每个源文件都需要在任何非注释行之前包含它。 标头不需要包含它,因为每个源文件都会在任何其他标头之前包含它。
Every source file needs to include it before any non-comment line. Headers do not need to include it, as every source file will include it before any other header.
您可以在项目级别或文件级别设置是否要使用预编译头文件。 对于项目设置,请转到项目属性对话框,然后单击配置属性/C/C++/预编译头。 有一个创建/使用预编译头的选项。 您可以通过在文件属性对话框中设置相同的选项来覆盖每个 .cpp 文件上的项目级别设置。
You can set whether you want to use a precompiled header file or not at the project level or a file level. For the project setting, go to project properties dialog and click Configuration Properties / C/C++ / Precompiled Headers. There is an option for Create/Use Precompiled Header. You can overwrite the project level setting on each .cpp file by setting the same option on file properties dialog.
所有 C/C++ 文件都需要在顶部有预编译头,是的 - 如果它是为该项目设置的。 但你可以根据 C/C++ 文件关闭/打开它(Kei 所说的)。
如果要包含标准头文件,请将它们全部放在预编译头文件中。 然后您需要在普通头文件 (.h) 中使用它们。 只要所有项目和文件都使用预编译头。
All you C/C++ files need to have your precompiled header at the top yes - if it set for that project. But you can have it turned off/on per C/C++ file (what Kei said).
If you have standard headers to include, place them all in the precompiled header. Then you want need them in the normal header (.h) files. As long as all projects and files are using precompiled headers.
正如其他人所说,只有您的源文件需要包含预编译头。 我只想补充一点,在我看来,无论有没有预编译头,代码都应该正确编译。 为了实现这一点,标头应该是自包含的,并且不依赖于包含其他所需标头的源文件。 我认为预编译头只是加速编译的一种方式,而不是项目中所有使用头的存储库。
As other have said, only your source files need to include the precompiled header. I would only add that, in my opinion, code should compile correctly with or without precompiled headers. To achieve this, headers should be self contained and not rely on the source files including other required headers. I see precompiled header only as a way to speed up the compilation and not as a repository of all used headers in a project.