为什么 stdafx.h 会这样工作?
和往常一样,当我的大脑搞乱了一些我自己无法弄清楚的事情时,我会向你们寻求帮助:)
这次我一直想知道为什么 stdafx.h 会这样工作? 据我了解,它做了两件事:
- 包括我们使用的标准标头 可能(?)使用 和 很少更改
- 何时用作编译器书签 代码不再预编译。
现在,这两件事对我来说似乎是两个非常不同的任务,我想知道为什么他们不采取两个单独的步骤来处理它们? 对我来说,让 #pragma-command 做书签工作并可选地在 windows.h 中使用一个头文件来包含常用的头文件似乎是合理的......这让我想到了下一个要点:为什么我们被迫通过 stdafx.h 包含常用的标头? 就我个人而言,我不知道我使用的任何经常使用的标头我还没有为其做自己的包含 - 但也许这些标头对于 .dll 生成是必需的?
提前谢谢
As usual, when my brain's messing with something I can't figure out myself, I come to you guys for help :)
This time I've been wondering why stdafx.h works the way it does? To my understanding it does 2 things:
- Includes standard headers which we
might (?) use and which are rarely changed - Work as a compiler-bookmark for when
code is no longer precompiled.
Now, these 2 things seems like two very different tasks to me, and I wonder why they didn't do two seperate steps to take care of them? To me it seems reasonable to have a #pragma-command do the bookmarking stuff and to optionally have a header-file a long the lines of windows.h to do the including of often-used headers... Which brings me to my next point: Why are we forced to include often-used headers through stdafx.h? Personally, I'm not aware of any often used headers I use that I'm not already doing my own includes for - but maybe these headers are necessary for .dll generation?
Thx in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
stdafx.h 是让 Visual Studio 执行预编译标头的一种方法。 这是一种使用简单、易于自动生成的方法,适用于较小的应用程序,但可能会给较大、更复杂的应用程序带来问题,因为它鼓励有效地使用单个头文件,因此可能会导致组件之间的耦合在其他方面都是独立的。 如果仅用于系统头文件,通常没问题,但随着项目规模和复杂性的增长,很容易在其中添加其他头文件,然后突然更改任何头文件结果是重新编译项目中的所有内容。
请参阅此处:有没有办法在 VC++ 中使用预编译头而不需要 stdafx.h? 了解替代方法的详细信息。
stdafx.h is ONE way of having Visual studio do precompiled headers. It's a simple to use, easy to automatically generate, approach that works well for smaller apps but can cause problems for larger more complex apps where the fact that it encourages, effectively, the use of a single header file it can cause coupling across components that are otherwise independent. If used just for system headers it tends to be OK, but as a project grows in size and complexity it's tempting to throw other headers in there and then suddenly changing any header file results in the recompilation of everything in the project.
See here: Is there a way to use pre-compiled headers in VC++ without requiring stdafx.h? for details of an alternative approach.
您不必被迫使用“stdafx.h”。 您可以在项目属性中(或创建项目时)勾选“使用预编译头”,这样您就不再需要
stdafx.h
了。编译器以它为线索,能够将最常用的头文件单独预编译在
.pch
文件中,以减少编译时间(不必每次都编译)。You are not forced to use "stdafx.h". You can check off the Use precompiled headers in project properties (or when creating the project) and you won't need
stdafx.h
anymore.The compiler uses it as a clue to be able to precompile most used headers separately in a
.pch
file to reduce compilation time (don't have to compile it every time).它可以缩短编译时间,因为其中的内容总是首先编译(请参阅下面引用中的详细信息):
It keeps the compile time down, as the stuff in it are always compiled first (see details in quote below):
它将有助于减少长时间的编译。
It will help reduce long compilations.