Why does Visual Studio C++ require including "StdAfx.h" even on files that don't need it?
Asked Answered
E

3

9

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.

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. If you forget to include StdAfx.h on one file, it's an error. 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." I don't understand why VC++ would require you to include StdAfx.h when it's not needed. 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?

Egyptology answered 18/1, 2017 at 2:32 Comment(6)
The whole point of precompiled headers, is to shorten the compilation time. If any additional checks would be done, it would increase the compilation time, rendering the sole benefit of precompiled headers - useless.Georgetown
Why do you say that? There's no need for an additional check -- VC++ could just do "if the first line is #include "stdafx.h" then copy those symbols in there; but otherwise do nothing". Seems like it would have been easier for Microsoft to do it that way and more obvious for users too.Egyptology
In fact, not requiring "StdAfx.h" on every file would actually improve compile time, since then it wouldn't need to copy those precompiled symbols on to the .cpp files that don't need them.Egyptology
@user2543623: "if the first line is #include "stdafx.h" then copy those symbols in there; but otherwise do nothing" You assume that precompiled headers have to be called "stdafx.h". Projects can actually have multiple, separate precompiled headers; each source file can designate which PCH it uses. Don't confuse the defaults that the typical project generator gives you with the totality of what the compiler can do.Sachsen
Select creating an empty project, otherwise it'll require to include stdafx.h every timeTranslunar
@LưuVĩnhPhúc Or change the project default after generation.Teeming
C
12

Just a addition to the Marks answer. In fact, you do not have to manually include stdafx.h in the all project source files. You may use project option Forced Include Files: enter image description here

That way stdafx.h will be automatically included in all your sources.

Cambrai answered 18/1, 2017 at 3:19 Comment(2)
Good point, just be sure if there is more than one forced include that the precompiled header is listed first, for the same reason it should be first in the source file....any code before it is ignored and the compiler starts with the state in the .pch file.Teeming
This older answer suggests (/FI) breaks Edit & Continue. Still the case?Monserratemonsieur
T
11

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:

enter image description here

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.

The project defaults are:

enter image description here

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." That's important and why stdafx.h should be included as the first line of the file. 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. 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.

If you have a file that you don't want to use pre-compiled information, you can select the file and override it. Example:

enter image description here

Visual Studio won't use the precompiled information and won't look for a header to include.

Teeming answered 18/1, 2017 at 2:52 Comment(6)
Technically, it does not answer OPs question, since he stated this, in the first sentence of his question: yes, I know I can turn them off. But that's not my question.Georgetown
I know, I'm working on an edit. I wasn't in front of my computer and wanted some screenshots. Plus OP may not know you can individually turn them off, which was my point. To quote the OP: [Visual C++ requires every cpp file to #include "StdAfx.h"] and it does not.Teeming
@AlgirdasPreidžius Well, you're assuming that the original poster knows that he can turn it off for specific files and not the whole project. My experience tells me that most people do not know that or how to do it. I work with people with advanced degrees who have been using the tools for over a decade and they don't know that. Whether he knows that or not is not clear. Mark gave useful information. the real answer is, "because microsoft said so."Quentinquercetin
OK, you can turn off StdAfx.h on individual files, but then why would VC++ require you to do that? If they're able to compile some files without stdAfx.h; then why can't it do the simple and obvious thing of using StdAfx.h if you include it and not using it if you don't include it? That would be the most obvious way of "turning it off per file", no?Egyptology
It's a contract. If you turn on the switch to use pre-compiled headers, you have to have stdafx.h as the first included file. Don't use it if you don't want it. If you don't like it, use g++ or write your own compiler.Quentinquercetin
What @Joe said :) If your default is "Use precompiled headers", VS must scan each file looking for the named precompiled header to know where to pick up where it left off in the pre-compiled .pch. If you don't want to perform that scan, don't use the "Use precompiled headers" switch when compiling that file.Teeming
C
1

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.

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 ;)

Clemenciaclemency answered 19/9, 2018 at 10:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.