Precompiled Headers: Including in .h files?

Started by
2 comments, last by mattnewport 16 years ago
Hey all. Recently I stared to use what is known as "precompiled headers". This technique is absolutely impressive and a must use for everyone( in my opinion). Compilation time has fallen from a over a minute to just a few secs. Impressive. Anyway. Straight to the question. What happens if I include (for windows) my stdafx.h file in ohter .h files? E.g. In my stdafx.h file I put all directX .h files in, and also all qt .h files in. Now, is it good to include this stdafx.h file in other .h files which use, lets say, just d3d9.h ? I mean, what happes at the linker stage. Is the stdafx.obj included in all .obj files, which use the stdafx.h file or is the stdafx.obj file just referenced in the ohter .obj files? I see some further improvements in including the stdafx.h file in other .h files. E.g. if I have to change a class name, which is included in stdafx.h instead of all .h files which use this class, I can just alter the name in the stdafx.h instead of altering this name in all .h files, which include the altered class... You see? Grateful for requests Alex
Advertisement
hi,

you do NOT want to include "stdafx.h" files in other header files.. it should not be included in any header files only in the .cpp file.

precompile headers creates a file "cache" of what headers you are using defined in your stdafx.h file. This good because if you use something like #include <stdlib.h> then the compile will only process it once. If you on the other hand include your stdlib in all header files then it will process it everytime it meets it.

A rule of thumb is to avoid using include in header files (they are not your friend)... forward declare everything that is possible.

hope this helped,
Peter Wraae Marino
http://osghelp.com - great place to get OpenScenGraph help
Hi and thanks for replying.

Foreward decls are bad in this way, that if I plan to change class names, I have to change it in every .h file I made it.
Till now, I had no problems with including the precompiled header stdafx.h in my
.h files.

Quote:
If you on the other hand include your stdlib in all header files then it will process it everytime it meets it.

Here I cant share your opinion. I did it, and I also put the following into the windows.h file:
#pragma message("Compiling windows.h - this should happen just once per project.\n")


And allthough I used stdafx.h within other .h files, the line above is just shown once during compilation!

I came to the conclusion, that it is possible to include the stdafx.h in other .h files. But, how is it exactly linked?

Alex
It's not linked at all. When you have precompiled headers enabled the compiler pre-parses all the headers included in the .cpp file that has the generate pre-compiled headers switch turned on (stdafx.cpp if you use VC's automatic settings) and generates a .pch file caching the pre-compiled code - this is not an object file or any documented format, it is presumably some binary serialization of state of the abstract syntax tree at that point in the compile. Other .cpp files that have the switch set to use that pre-compiled header will then read in the .pch file and save the IO and CPU cost of reading and parsing all those headers.

The best way to organize your code is to #include the stdafx.h as the only line in the stdafx.cpp and then #include it as the first line of all your other .cpp files. If you have a large application made up from multiple projects then you might want to try having a .pch per project to avoid the .pch becoming very large due to pulling in the union of all the headers required by the individual projects.

Game Programming Blog: www.mattnewport.com/blog

This topic is closed to new replies.

Advertisement