views:

249

answers:

1

If I put a header (a.h) into stdafx.h and that header includes another header (b.h) that is not mentioned in stdafx.h, will b.h be visited every time someone includes a.h or is it compiled in as part of a.h? If it is compiled into a.h, what happens when someone includes b.h directly? Will this be precompiled or not?

My motivation for asking this question is that I am trying to optimize the content of the stdafx.h files for the software that I work on. Both rebuild and incremental build times are important to us. I was wondering whether I could simply search through all of the .cpp files for #include directives and count the number of times each file is included. Files which were included often might be good candidates for the stdafx.h file. Of course, this strategy is completely bogus if I have to consider not only which files are included, but also which files the included files include.

I doubt it matters, but we are using Visual Studio 2005.

+3  A: 

a.h and b.h will be part of precompiled header, and there is no need to include them later. All you need is to include stdafx.h where a.h or b.h are required. If you'll include a.h or b.h explicitly after stdafx.h (all code before stdafx.h include are ignored), then it'll not be compiled second time (just because they are usually protected by #pragma once directive or defines), but compiler will open that file on hard disk if you are asking about it,

By the way, you should know, that you could use several precompiled header files (but no more than one in every cpp file).

Kirill V. Lyadvinsky
What you say about including a.h or b.h after stdafx.h is technically true -- the compiler will look at the headers again -- but most headers use #pragma once or include guards to prevent this from happening. In fact, I would recommend including a.h and b.h anywhere they are needed even if a.h is in stdafx.h -- otherwise you won't be able to tweak the contents of stdafx.h later without running into an endless list of compile errors.
Nick Meyer
Yes, they will not be compiled, but they will be opened by compiler technically.
Kirill V. Lyadvinsky
I agree Nick's idea is helpful, but I am just afraid there would be performance hit as we need to do additional disk IO to find there is a #progma once
lz_prgmr