Question

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.

Was it helpful?

Solution

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top