Should precompiled header files also be included in source header files?

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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.

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

Code:

#pragma once

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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
C, C++ Compiler: Microsoft VS2015

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.