include stdafx.h in header or source file?
Asked Answered
L

2

16

I have a header file called stdafx.h and this one is precompiled of course. I've read that I should include these files into my .cpp files, but some of these statements are already needed in the header file coming with that.

Should I add the stdafx into my header or into my cpp? I thought it was good practise to put it into the header, but I seem to be obliged to put it into the header instead.

Example:

stdafx contains freeglut.
my class header file has an attribute of GLenum.

Should I include the stdafx into the .h of the class?

Logue answered 8/3, 2011 at 15:12 Comment(0)
H
31

stdafx.h should be the first include in EVERY cpp file in your project.


Consider that C++ doesn't compile header files, just Cpp files.

Therefore if the stdafx is the first include in the cpp file, then the compiler will have everything the header file needs, when it hits the header-file in the Cpp file.

e.g.

You have A.cpp & A.h.
A.h needs std:string.

you have B.cpp & B.h
B.h needs A.h therefore B.h needs std::string too.

Because it's good practice, you put #include <string> in stdafx.h.

Your build fails because nothing can see std::string

Now put stafx.h as the first include in A.cpp and B.cpp.
When the compiler hits A.cpp, it picks up the include for <string>, then picks up A.h, and everything is happy because we know what std::string is.

The compiler now hits B.cpp, again it includes stdafx first, which brings <string>, then hits B.h, which brings A.h which is again happy because std::string has already been included.

Hope this helps.

Homeomorphism answered 8/3, 2011 at 15:15 Comment(6)
Your comment that "C++ doesn't compile header files, just Cpp files" either doesn't make any sense, or is plain wrong. Please clarify.Treacle
@John: I'm trying to say, the compiler will never look at a .h file on it's own, it will only look at a .h file in the context of where it is included in a cpp file.Homeomorphism
Gotcha. That's generally true, though I'm sure you could force a compiler to compile an .H file directly as a translation unit all its own. And in fact this is, in a way, the whole point of PCH's. But when the compiler "looks at" an H file via an #include statement, it is compiled.Treacle
@John: I'm trying to simplify things for the asker. I've found in the past that the "lie" that only Cpp files get compiled is often a helpful learning tool. It helps folks stop from tying themselves in knots trying to figure out why the compiler suddenly complains about a header file that was working perfectly yesterday (e.g. it turns out they'd switched the include order in a new module, however they spend an hour looking at the header file, not how/what/where it's included).Homeomorphism
Strange that I didn't think of that. Seems pretty logical that stdafx is in front of the .h file, so it will also find the stdafx. Thanks for the good-practise tips.Logue
That bold text. Now I understand why you dont include it.Leighannleighland
O
6
  • Only include things in your precompiled header which should be there
  • Your precompiled header file must be the first include in every .cpp
  • I would avoid including it in another header in favor of forward declaration

Ask yourself these two questions before including something in stdafx.h

  1. Will this header never be changed by me?
  2. Do I need this included in every multiple source file?

If the answer is "No" to either of those then don't include it.

Olivas answered 8/3, 2011 at 15:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.