Sign in to follow this  

Precompiled Headers: Including in .h files?

This topic is 3548 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3548 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this