为什么 stdafx.h 会这样工作?

发布于 2024-07-10 15:36:09 字数 443 浏览 14 评论 0原文

和往常一样,当我的大脑搞乱了一些我自己无法弄清楚的事情时,我会向你们寻求帮助:)

这次我一直想知道为什么 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

み格子的夏天 2024-07-17 15:36:10

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.

柠檬色的秋千 2024-07-17 15:36:10

您不必被迫使用“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).

此岸叶落 2024-07-17 15:36:10

它可以缩短编译时间,因为其中的内容总是首先编译(请参阅下面引用中的详细信息):

stdafx.h是一个文件
描述了两个标准系统
和项目特定的包含文件
经常使用但很少使用的
曾经改变过。

兼容的编译器将
预编译该文件以减少
总体编译时间。 Visual C++ 将
在之前不编译任何东西
#include "stdafx.h" 在源文件中
,除非编译选项
/Yu'stdafx.h' 未选中(通过
默认); 它假设所有代码在
源直到并包括该行
已经编译好了。

It keeps the compile time down, as the stuff in it are always compiled first (see details in quote below):

stdafx.h is a file
that describes both standard system
and project specific include files
that are used frequently but hardly
ever changed.

Compatible compilers will
pre-compile this file to reduce
overall compile times. Visual C++ will
not compile anything before the
#include "stdafx.h" in the source file
, unless the compile option
/Yu'stdafx.h' is unchecked (by
default); it assumes all code in the
source up to and including that line
is already compiled.

软糯酥胸 2024-07-17 15:36:10

它将有助于减少长时间的编译。

It will help reduce long compilations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文