简体   繁体   English

为什么Visual Studio C ++甚至在不需要的文件上也要求包含“ StdAfx.h”?

[英]Why does Visual Studio C++ require including “StdAfx.h” even on files that don't need it?

I understand what precompiled headers are doing with "#include "StdAfx.h" and yes, I know I can turn them off. But that's not my question. 我知道使用“ #include” StdAfx.h“预编译的标头在做什么,是的,我知道我可以将它们关闭。但这不是我的问题。

If you're using precompiled headers, Visual C++ requires every cpp file to #include "StdAfx.h", even the files that aren't using any of the headers in StdAfx.h. 如果您使用的是预编译头文件,则Visual C ++要求每个 cpp文件都必须#include“ StdAfx.h”,即使文件没有使用StdAfx.h中的任何头文件。 If you forget to include StdAfx.h on one file, it's an error. 如果您忘记在一个文件中包含StdAfx.h,那就是错误。 But why? 但为什么? The obvious approach would be just "If you include StdAfx.h then that file will use it, but if you forget to include it, then those header files will simply not be included." 显而易见的方法是“如果包含StdAfx.h,则该文件将使用它,但是如果您忘记包含它,则将不包含那些头文件。” I don't understand why VC++ would require you to include StdAfx.h when it's not needed. 我不明白为什么VC ++在不需要时会要求您包含StdAfx.h。 Seems like it would have been easier for them to treat it like a normal header file. 似乎对于他们来说,将其像普通的头文件一样容易处理。

Is there any good reason why this is required? 是否有充分的理由要求这样做?

Your project default is "use precompiled headers". 您的项目默认值为“使用预编译头”。 You can set individual files to "not use precompiled headers" if you desire. 如果需要,可以将单个文件设置为“不使用预编译头”。

In fact, stdafx.cpp itself has a different option from the project defaults: 实际上, stdafx.cpp本身具有与项目默认设置不同的选项:

在此处输入图片说明

What this configuration is saying is "start compiling this file (stdafx.cpp), stop when you finish compiling the statement that includes stdafx.h" and save the precompiled information as as .pch file." Visual studio is also smart enough to compile this file first so it is available for use. 这种配置的意思是“开始编译该文件(stdafx.cpp),在完成对包含stdafx.h的语句的编译时停止”,并将预编译的信息另存为.pch文件。首先使用此文件,以便可以使用。

The project defaults are: 项目默认值为:

在此处输入图片说明

What this configuration is saying is "For each compiled file, start with the precompiled data in the specified .pch and start compiling new information after the point stdafx.h is included." 这种配置的意思是“对于每个编译文件,从指定的.pch中的预编译数据开始,并包含stdafx.h点之后开始编译新信息。” That's important and why stdafx.h should be included as the first line of the file. 这很重要,为什么要将stdafx.h作为文件的第一行包含在内。 It will still work if you put it later in the file, but anything before the #include is ignored because that data won't be in the .pch. 如果稍后将其放在文件中,它仍然可以工作,但是#include之前的任何内容都将被忽略,因为该数据不会位于.pch中。 Absence of the #include means VS will scan the whole file looking for the pre-compiled starting location and not find it...generating an error. 缺少#include意味着VS将扫描整个文件以查找预编译的起始位置,但找不到它...生成错误。

If you have a file that you don't want to use pre-compiled information, you can select the file and override it. 如果您有不想使用预编译信息的文件,则可以选择该文件并将其覆盖。 Example: 例:

在此处输入图片说明

Visual Studio won't use the precompiled information and won't look for a header to include. Visual Studio不会使用预编译的信息,也不会寻找要包含的标头。

Just a addition to the Marks answer. 只是对马克答案的补充。 In fact, you do not have to manually include stdafx.h in the all project source files. 实际上,您不必在所有项目源文件中手动包含stdafx.h You may use project option Forced Include Files : 您可以使用项目选项“ Forced Include Files 在此处输入图片说明

That way stdafx.h will be automatically included in all your sources. 这样, stdafx.h将自动包含在您的所有源代码中。

When you select the file, right-click properties, go to the "C/C++ \\ Precompiled Headers" section and set "Precompiled Header" to "Not using Precompiled Headers", be sure that the Configuration (top left) is applicable to the current selected build. 选择文件后,右键单击属性,转到“ C / C ++ \\ Precompiled标头”部分,并将“ Precompiled标头”设置为“不使用预编译标头”,请确保该配置(左上方)适用于当前选择的版本。

It doesn't always automatically select the "active" configuration; 它并不总是自动选择“活动”配置。 so you could be setting the option for a non-active configuration so you will continue to experience the error ;) 因此您可以为非活动配置设置选项,这样您将继续遇到错误;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio-为什么要嵌入stdafx.h - Visual Studio - why does stdafx.h gets embedded 为什么我必须在Microsoft Visual Studio C ++应用程序中#include“ stdafx.h”? - Why do i have to #include “stdafx.h” in Microsoft visual studio c++ applications? 为什么MS Visual Studio #include <stdio.h> 默认情况下在stdafx.h中? - Why does MS Visual Studio #include <stdio.h> in stdafx.h by default? 如何防止Visual C ++创建stdafx.h? - How to prevent Visual C++ from creating stdafx.h? 用C ++编码时,可以在Visual Studio中删除stdafx.h吗? - could you remove stdafx.h in visual studio when coding in c++? C ++找不到stdafx.h - C++ cannot find stdafx.h 我可以使用#include“pch.h”而不是#include“stdafx.h”作为Visual Studio C ++中的预编译头吗? - Can I use #include “pch.h” instead of #include “stdafx.h” as my precompile header in Visual Studio C++? 为什么我不能运行这个为什么这个 C++ 代码在 Visual Studio 中给出错误,尽管它在删除 #include &quot;stdafx.h&quot; 后在代码块中运行良好 - why I cant run this why this c++ code giving errors in visual studio ,though it is running well in code blocks after deleting #include "stdafx.h" 使用opencv将stdafx.h文件包含在C ++程序中的目的是什么? - What is the purpose of including the stdafx.h file in a C++ program using opencv stdafx.h到头文件 - stdafx.h to header files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM