-
September 16th, 2013, 09:59 AM
#1
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.
-
September 16th, 2013, 10:03 AM
#2
Re: Should precompiled header files also be included in source header files?
As long as you're including stdafx.h, no.
-
September 16th, 2013, 10:08 AM
#3
Re: Should precompiled header files also be included in source header files?
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?
-
September 16th, 2013, 10:11 AM
#4
Re: Should precompiled header files also be included in source header files?
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.
-
September 16th, 2013, 10:13 AM
#5
Re: 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?
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...).
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
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
September 16th, 2013, 10:51 AM
#6
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
Last edited by AnotherMuggle; September 16th, 2013 at 10:57 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|