Should precompiled header files also be included in source header files?
When including a header file in stdafx.h, should that file still be included in the source file where it is actually used?
I've done lots of searching but I can't convince myself either way...
If it is included in both places, is the one in the source file ignored?
Cheers,
AnotherMuggle.
Re: Should precompiled header files also be included in source header files?
As long as you're including stdafx.h, no.
Re: Should precompiled header files also be included in source header files?
Quote:
Originally Posted by
GCDEF
As long as you're including stdafx.h, no.
Is that, no you shouldn't, or no you don't have to?
Re: Should precompiled header files also be included in source header files?
Quote:
Originally Posted by
AnotherMuggle
Is that, no you shouldn't, or no you don't have to?
Both. #include essentially just inserts the included file into the code where the #include statement is placed. When you #include stdafx.h, you include it, and any files that it #includes, so #including a file in stdafx.h and in your code is redundant. Depending on your use of include guards, the the compiler will use the file the first time it sees it, and either ignore it after that or generate errors.
Re: Should precompiled header files also be included in source header files?
Quote:
When including a header file in stdafx.h, should that file still be included in the source file where it is actually used?
If a header file is included in stdafx.h and stdafx,h is included in the program, then you don't need to also include the header file in the source file as includes can be nested (ie an include file can have an include file which can have an include file...).
Quote:
If it is included in both places, is the one in the source file ignored?
This depends upon how the include file is coded. If it starts with #pragma once or uses include guards then yes, the one in the source file is ignored. If the include file doesn't take into account being included multiple times then no, its not ignored. The compiler doesn't automatically ignore multi includes.
Well written header files usually start with
if the complier supports this
or an include guard
Code:
#ifndef headerxyz
#define headerxyz
//include code
#endif
Re: Should precompiled header files also be included in source header files?
Thanks both for the helpful answers.
Are static libraries an exception in any way? This issue relates closely to my other thread here: http://forums.codeguru.com/showthrea...static-library but I felt the question was different enough to start a new thread.
Basically if I include this in stdafx.h:
Code:
#ifdef MFC_BUILD
#include <afx.h>
#else
#include <atlstr.h>
#endif
but nowhere else in the library then MFC builds compile and link fine. However, non-MFC builds do not compile and the errors all revolve around CString being an undeclared identifier. If I duplicate the above include in each of the headers where CString is used, then the errors go away, the build completes and everything seems fine.
I'm stumped :(