views:

170

answers:

3

Looking at an open source code base i came across this code:

#include "StableHeaders.h"
#include "polygon.h"
#include "exception.h"
#include "vector.h"
...

Now the StableHeaders.h is a precompiled header which is included by a 'control' cpp to force it's generation. The three includes that appear after the precompiled header are also included in the StableHeaders.h file anyway. My question is, are these files included twice so that the code base will build on compilers that don't support precompiled headers? As im assuming that include guards/header caching will make the multiple includes redundant anyway...

EDIT btw, the stableheaders.h file has a check for win32 (roughly) so again im assuming that the includes inside stableheaders.h wont be included on compilers that don't support precompiled headers.

+2  A: 

Compilers that don't support precompiled headers would just include StableHeaders.h and reparse it every time (rather than using the precompiled file). It won't cause any problems neither does it fix any problems for certain compilers as you asked. I think its just a minor 'mistake' that probably happened over time during development.

Brian Ensink
Ok good advice, ill keep the question open to see other comments, thanks
Adam Naylor
And I assume #pragma hdrstop will be ignored by compilers that don't support it?
Adam Naylor
#pragma is a non-standard feature. The standard practice is to include the header conditionally using "#ifndef HEADER then #define HEADER"
Vijay Mathew
@Adam Naylor, #pragma lines are generally compiler specific, you can't assume other compilers will just ignore it; in fact the compiler is supposed to generate errors when it finds things it does not understand. Use preprocessor macros to guard platform specific sections of the code.
Brian Ensink
@Vijay http://developer.apple.com/documentation/DeveloperTools/gcc-4.0.1/cpp/Pragmas.html states that #pragma is standard? at least with C. Could you clarify?
Adam Naylor
@Adam Naylor: #pragma is a standard way to pass compiler specific information to the compiler. The values you can specify in a #pragma varries from platform to platform.
Brian Ensink
Yes, usage of #pragma is standard in C, but most options #pragma is used with are non-standard. C99 standard defines only 3 standard #pragma options (FENV_ACCESS, FP_CONTRACT, CX_LIMITED_RANGE); everything else is compiler specific.
nagul
Adam Naylor
A: 

I think you yourself answered the question question! Pre-compiled headers is a compiler feature. If the guard is present the headers will not be included twice, in any case.

Vijay Mathew
A: 

The only reason I can think of to protect the precompiled header and include the stuff anyway is speed. The reason to use precompiled headers is to speed up the compile times, this works by including and compiling the contents of the precompiled header, when you do this you can include headers that are only used by 75% of the source files and it is still quicker than no precompiled headers.

However if the other platforms down support prepcompiled headers you only want to include the header files that are required for this source file. So if the precompiled header contains include files that are only required for by some source files it is quicker to just include and compile the header files you need.

iain