Why a specific order with preprocessors #

I'm using Visual C++ 2005 express now as I go through my schooling. I realize there's a header that has to be included as a preprocesor #include "stdafx.h" ,however from what I have read and learned so far about how preprocessors work is that they are the first thing that the compiler reads and examines first before anything else. If this is so... I would imagine that all preprocessors are read first, but do they need an order? ie:

I have noticed that in visual C++ I get a compiler error if the stdafx.h is not first before any other preprocessors.

#include <iostream>
#include "stdafx.h" //this would cause a compiler error for me


#include "stdafx.h" //this would not cause errors
#include <iostream>


can someone please explain why this order has to exist? ... many thanks
closed account (z05DSL3A)
My guess is that you are using precompiled headers (VC default to this), as a result the first thing that VC looks for is stdafx.h to tell it 'where/what' the precompiled headers are. If it doesnt find this file first it errors.

HTH
Last edited on
cool..thanks for the info. Is there any way to change this, or is it just how VC works?
I've seen a similar problem to this. I did try to get to the bottom of it, it appeared to be to do with the #ifdef defines in stdafx.h setting up prerequisites for the other header files. I found no way of changing it that was worth the effort of pursuing.
You are getting stdafx.h because you are incorrectly creating your project as a Win32 Windows Console Application instead of a Win32 Console Application.

For a C++ console application do this:
1) Create a new Win32 project
2) When the wizard appears do not click finish
3) Instead click Application settings
4) Then select a) console application and b) empty project
5) Now click finish.

stdafx.h (the Microsoft Standard Application Framework eXtension.h) is a Microsoft-specific file. Unless you are writing Windows code, you don't need this header.

Topic archived. No new replies allowed.