C++ stdafx.h 的目的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2976035/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:38:58  来源:igfitidea点击:

Purpose of stdafx.h

c++visual-c++precompiled-headersstdafx.h

提问by ckv

What is the purpose of the file stdafx.hand what is meant by precompiled headers?

文件的用途是stdafx.h什么,预编译头是什么意思?

采纳答案by Jorge Ferreira

stdafx.his a file, generated by Microsoft Visual Studio IDE wizards, that describes both standard system and project specific include files that are used frequently but hardly ever change. Compatible compilers (for example, Visual C++ 6.0 and newer) 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.

stdafx.h是一个由 Microsoft Visual Studio IDE 向导生成的文件,它描述了标准系统和项目特定的经常使用但几乎不会更改的包含文件。兼容的编译器(例如 Visual C++ 6.0 和更新版本)将 预编译此文件以减少总编译时间

Visual C++ 不会#include "stdafx.h"在源文件中的之前编译任何内容 ,除非/Yu'stdafx.h'未选中编译选项 (默认情况下);它假定源代码中直到并包括该行的所有代码都已编译。

http://en.wikipedia.org/wiki/Precompiled_header

http://en.wikipedia.org/wiki/Precompiled_header

回答by Mark Ransom

To expand on the other excellent answers:

扩展其他优秀答案:

stdafx.his the file that includes all of the commonly used headers for a single project. This would include all of the Windows definitions, for example. Because this file includes so much stuff, the compiler gets a bit slow when processing it. By precompiling it, the compiler can skip much of the processing and reuse it over and over again; as long as none of the files included by it change, the precompiled result doesn't need to change either.

stdafx.h是包含单个项目的所有常用头文件的文件。例如,这将包括所有 Windows 定义。因为这个文件包含很多东西,编译器在处理它时会变得有点慢。通过预编译,编译器可以跳过大部分处理并一遍又一遍地重用它;只要它包含的文件都没有改变,预编译结果也不需要改变。

The name stdafx.his just a convention. You could easily rename it to something else if you changed all your sources to include the new file instead.

这个名字stdafx.h只是一个约定。如果您更改了所有源以包含新文件,则可以轻松地将其重命名为其他名称。

To produce the actual precompiled header file, you need one source file in the project that has special compile flags to produce precompiled output. By convention this file is named stdafx.cpp, and if you inspect the settings for that source file you will see how it is different.

要生成实际的预编译头文件,您需要项目中的一个源文件具有特殊的编译标志来生成预编译输出。按照惯例,此文件名为stdafx.cpp,如果您检查该源文件的设置,您将看到它有何不同。

回答by Brian R. Bondy

It's typically used for the name of precompiled headers. Although using that exact name is not required, just the default. I explain more about pre-compiled headers on VC++ and g++ here.

它通常用于预编译头的名称。尽管不需要使用该确切名称,但只需使用默认名称即可。 我在这里解释了更多关于 VC++ 和 g++ 的预编译头文件

You use precompiled headers for faster compilation.

您可以使用预编译头文件来加快编译速度。

The idea is that you put any header file that will not change, and that you use in several source files inside your precompiled header. Then the compiler will not need to reprocess those headers for each compilation unit.

这个想法是你把任何不会改变的头文件放在你的预编译头中的几个源文件中。这样编译器就不需要为每个编译单元重新处理这些头文件。

回答by Pavel Minaev

It's a precompiled header, to reduce compilation times.

这是一个预编译的头文件,以减少编译时间。