I want to reduce compile time on a large project. Our primary compiler is Visual Studio 2010 but some of the code gets compiled in gcc. We are currently planning to ensure that all our .h files have both include guards as well as #pragma once, this will allow both Visual Studio and gcc to improve compile speed. Previously we had put more headers in the stdafx but we saw disadvantages that if one of those headers was changed, and you compiled a cpp without recompiling the precompiled header that the changes didn't take effect. This often caused us confusion. The current plan is to use precompiled headers for all stable headers or headers out of our control (they won't change) and for everything else use the include guards and #pragma once to help on compilation speed. Is there a reason why this path is poorly planned? Is there a benefit for compilation speed of include guards/#pragma once vs precompiled header or vise-versa that I am missing?
Asked
Active
Viewed 1,018 times
2
-
1I might be missing something, but I don't understand this either or approach. Nearly all header files should have include guards and precompiled headers should be used where appropriate. Neither is exclusive of the other. – IronMensan Nov 18 '11 at 14:15
-
1You are right neither they are not exclusive. The last question should be clarified to really say does one versus the other have an advantage related to compilation speed. I will edit the question. – casey_tx Nov 18 '11 at 14:20
1 Answers
2
The approach that you are taking is sound, but if changes in one of the headers did not trigger recompilation of the precompiled headers you should check the dependencies in the project.
There are other things that can help in reducing compilation times, like avoiding the includes altogether. That is, use forward declarations in the headers and only include in the cpp files. That will reduce the compile time dependencies and speed up compilation.
I am not a fan of precompiled headers, so I usually just ensure that I include everything that needs including and don't include anything that doesn't.

David Rodríguez - dribeas
- 204,818
- 23
- 294
- 489
-
2After performing some more testing in a test project I have found a significant effect from precompiled headers. In the project I have ensured that all the .h files have #pragma once. When I include some very core .h files in the precompiled header the compile time is ~0:00:40 . If I take out those files from the precompiled header and only expect #pragma once to help my compilation then the same project takes ~0:02:20. So for us the compile time of having the files in the precompiled header probably overweighs the compile confusion. – casey_tx Nov 18 '11 at 16:04