Why do we use `#include "stdafx.h"` instead of `#include <stdafx.h>`?
Asked Answered
P

3

7

From here, it is said that:

For #include "filename" the preprocessor searches in the same directory as the file containing the directive. This method is normally used to include programmer-defined header files.

For #include <filename> the preprocessor searches in an implementation dependent manner, normally in search directories pre-designated by the compiler/IDE. This method is normally used to include standard library header files.

While this wiki link suggests that stdafx.h is an header file pre-designed by visual studio IDE

stdafx.h is 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 precompile 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.

The AFX in stdafx.h stands for Application Framework eXtensions. AFX was the original abbreviation for the Microsoft Foundation Classes (MFC). While the name stdafx.h is used by default, projects may specify an alternative name.

Then

Why do we use #include "stdafx.h" instead of #include <stdafx.h> ?

Proposal answered 16/5, 2016 at 15:36 Comment(1)
Perhaps where Visual Studio default-generates that header may be of some assistance in answering your own question.Zolner
C
9

A stdafx.h, stdafx.cpp pair is generated by VS from a template. It resides in the same directory the rest of the files end up. You will probably end up altering it specifically for your project. So we use "" instead of <> for exactly the reason that it's in the same directory as your first quote describes.

Calculous answered 16/5, 2016 at 15:39 Comment(4)
OK, I see stdafx.obj, stdafx.h, stdafx.cpp in the ...\testMEX\testMEX\Debug and ...\testMEX\testMEX where testMEX is the name of my console application projectProposal
so you mean 1- the files stdafx.h, stdafx.cpp, handler_stdafx.h, handler_stdafx.cpp in different subfolders (.NET, ATL, Generic, MFC) of C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\VCWizards\AppWiz 2- the files Stdafx.h, StdAfx.h, Stdafx.cpp, StdAfx.cppin different parts of the directory C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Extensibility Projects 3- the files stdafx.h, stdafx.cpp in different subfolders (include, lib, src) of C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\atlmfc directory 4- the files stdafx.h,Proposal
stdafx.cpp in C:\Program Files (x86)\Microsoft Visual Studio 10.0\DIA SDK\Samples\DIA2Dump directory in my Win7x64 machine are all templates. And when we start a new project in Visual Studio 2010 a special stdafx.h file is created for that based on the type of the projects and using these templates?Proposal
I've included #include "stdafx.h" in my code but I'm getting the errors 'cannot open source file "stdafx.h"' and 'Cannot include file: "stdafx.h": No such file or directory. Does anybody know how I can solve this?Sharika
U
4

Because stdafx.h is different for each project. As you quoted, #include "" searches the path of the current project, and this is where stdafx.h is located.

Using #include <stdafx.h> would be a huge mistake, because it would have to be in the library path (where all the standard library headers are located). This would mean that you shouldn't modify it, or that it always stays the same, but it's never the same for different projects.

So even though it is generated by Visual Studio, it is specific to the project, not to all projects.

Ungrounded answered 16/5, 2016 at 15:41 Comment(0)
C
0

In general, for a given include file name.h, the syntax #include <name> is reserved for the standard libraries, while #include "name.h" is used for user-defined files. There, 'user' could mean any developer implementing non-standard features, say for a particular compiler package.

Accordingly, the preprocessor searches for the include files following the suitable paths specified by the system settings in place. Note not only the difference between brackets and quotes, but also the different usage of the *.h extension.

Craze answered 28/3, 2018 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.