Stdafx + Header File - Order of Inclusion in Mfc Application

StdAfx + Header file - Order of inclusion in MFC application

This link must give you some clue.
Purpose of stdafx.h

The lines defined before the
#include "stdafx.h" are ignored by the compiler. So if you want to actually include those files then you need to include them after the #include "stdafx.h".

Hope it is clear.

Why stdfax.h should be the first include on MFC applications?

It's only true when using Precompiled Headers (PCH), and the reason why there shouldn't be anything before the #include "stdafx.h" is :

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

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

C/C++ include header file order

I don't think there's a recommended order, as long as it compiles! What's annoying is when some headers require other headers to be included first... That's a problem with the headers themselves, not with the order of includes.

My personal preference is to go from local to global, each subsection in alphabetical order, i.e.:

  1. h file corresponding to this cpp file (if applicable)
  2. headers from the same component,
  3. headers from other components,
  4. system headers.

My rationale for 1. is that it should prove that each header (for which there is a cpp) can be #included without prerequisites (terminus technicus: header is "self-contained"). And the rest just seems to flow logically from there.

Purpose of stdafx.h

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
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.

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

Error C1083: Cannot open include file: 'stdafx.h'

You have to properly understand what is a "stdafx.h", aka precompiled header. Other questions or Wikipedia will answer that. In many cases a precompiled header can be avoided, especially if your project is small and with few dependencies. In your case, as you probably started from a template project, it was used to include Windows.h only for the _TCHAR macro.

Then, precompiled header is usually a per-project file in Visual Studio world, so:

  1. Ensure you have the file "stdafx.h" in your project. If you don't (e.g. you removed it) just create a new temporary project and copy the default one from there;
  2. Change the #include <stdafx.h> to #include "stdafx.h". It is supposed to be a project local file, not to be resolved in include directories.

Secondly: it's inadvisable to include the precompiled header in your own headers, to not clutter namespace of other source that can use your code as a library, so completely remove its inclusion in vector.h.

Microsoft Visual Studio 17 errors compiling

For reasons I am not privy to, the compiler in Visual Studio ignores anything above the include of stdafx.h.

Solution: Reorder the includes to place #include "stdafx.h" at the top.

Alternate Solution: Remove #include "stdafx.h" and disable precompiled headers.

The following may be helpful reading: Purpose of stdafx.h

Namespace declared in Header file is not being recognized in Source File

stdafx.h (Microsoft precompiled header) must be at the top. This applies to any Visual C++ project with the precompiled headers option turned on, and stdafx.h the standard pch. Those are the default settings for a new project.

Purpose of stdafx.h

The simplest and least error-prone way to define the function within the namespace App is just to
put it there.

APP.CPP

#include "stdafx.h" // Nothing goes above this
#include "App.h"
#include <Graphic\Graphic.h>

namespace App {
void frameLoop()
{
while (state != AppStatus::Exit) {
Graphic::renderingSequence();
}
}
}


Related Topics



Leave a reply



Submit