stdafx.h - what's it all about?

  • Thread starter Thread starter Peter Oliphant
  • Start date Start date
P

Peter Oliphant

I know that 'stdafx.h' has to do with pre-compiled headers. However, it seem
weird that it is a required 'include' in any CPP file, especially since the
file itself has no code in it!

I know that 'stdafx.h' is a good place to put "include files that are used
frequently, but are changed infrequently" .

But why does this file HAVE to be included when it contains nothing?

Just curious about this. I just include it like always and therefore this
has not caused any problems... : )

However, its my personal style to put all class code IN the header, thereby
making all such class code in-line. Of course, there are times this can't be
done, like when two classes need to reference each other's methods or
members (i.e., other than just a pointer to an instance of, say, 'myClass',
which can be satisfied via the 'class myClass;' statement just to let the
compiler know that 'myClass' is, indeed, a class that can be pointed to).

[==P==]
 
stdafx.h includes all kinds of defines and includes to other files. It's
needed wherever those files are needed. Mostly it includes all of the afx
support stuff, thus the name stdafx.h.

Tom
 
Peter said:
I know that 'stdafx.h' has to do with pre-compiled headers. However, it seem
weird that it is a required 'include' in any CPP file, especially since the
file itself has no code in it!

I know that 'stdafx.h' is a good place to put "include files that are used
frequently, but are changed infrequently" .

But why does this file HAVE to be included when it contains nothing?

Just curious about this. I just include it like always and therefore this
has not caused any problems... : )

However, its my personal style to put all class code IN the header, thereby
making all such class code in-line. Of course, there are times this can't be
done, like when two classes need to reference each other's methods or
members (i.e., other than just a pointer to an instance of, say, 'myClass',
which can be satisfied via the 'class myClass;' statement just to let the
compiler know that 'myClass' is, indeed, a class that can be pointed to).

[==P==]
You don't have to use PCH, you can always set the "Dont Use PreCompiled
Headers" option in VC.net.
 
I remember that I have read the following explanation long long time ago:

Here is what happens when you use precompiled headers:

a) When the precompiled header base file (stdafx.cpp) is compiled with /Yc,
the file gets compiled, and once this has happened, the virtual memory of
the compiler process including all internal preprocessor macro tables etc,
is just mapped to a file - the precompiled header file.
b) When a file is compiled with the flag /Yu that tells the compiler, a
precompiled header should be used, the first thing the compiler does now, is
to map the precompiled header containing all internal preprocessor tables
into the virtual memory, instead of initializing them as empty tables and
processing the precompiled header files. This obviously is only possible, if
the #include for the precompiled header is the first thing that happens.

Obviously it is much faster to map a file into memory than to process a
complex header file including many other headers. Exactly that's the purpose
of precompiled headers.

If your precompiled header does nothing, there is no performance benefit,
and you can just turn it off via the project properties.

Hope this helps

Marcus Heege
www.heege.net
 
Back
Top