Speedier Visual C++ Compiles

As I mentioned recently, I’ve been having some issues with the speed of Visual C++ compiles. Some adjustments to my VMware Fusion settings reduced the time it takes for a complete rebuild of my project, from roughly an hour and five minutes to about 43 minutes, but I knew there was still room for improvement.

Several of the sub-projects had monolithic header files that each source code file was including; I broke these up and set each source code file to only include the ones that it was actually using. That helped some, because fewer source code files had to be compiled most of the time, but it wasn’t enough.

Several of the sub-projects shared many source code files, and the same settings. The shared files were being compiled separately for each subproject, which was inefficient. I gathered all the common source code files into several static libraries, which eliminated the redundant compiles. Again, it helped, but not enough.

Finally, we get to the subject of precompiled headers.

I never had much use for precompiled headers. They never seemed to do much to reduce my compile times… in fact, they seemed to increase my compile times, which is why I had them turned off in all of these projects. But I found a page that explained the proper use of them (hint: MSVC’s default setup doesn’t use them right).

In a nutshell:

  • Create a single header file (I’ll call it precompiled.h here) which #includes only those header files that never (or almost never) change — windows.h, stdio.h, the STL library, the Boost library, etcetera. Add it to your project.
  • Create a source code file (I’ll call it precompiled.cpp, to go along with precompiled.h) that contains nothing but one line, #include "precompiled.h". Add it to your project too.
  • Add #include "precompiled.h" as the very first non-comment line in all source code files in the project. This is vital, because MSVC will ignore anything before it.
  • Set the entire project to use (not create!) precompiled headers, and set the PrecompiledHdrs.cpp file to create them.

The result: the compiles (after the precompiled.cpp file, when it needs compiling, which is seldom) are now ludicrously fast — at least ten times as fast as they were without precompiled headers! I assume most of that speed increase comes from the fact that I use a lot of stuff from the Boost library, but precompiled headers should help a lot even if you’re only using the raw Windows API functions (windows.h is huge, and pulls in a bunch of other header files too).

So compiles are now blazingly fast. Not as fast as I’d get on a good desktop machine (or even this one running on bare metal instead of virtualized), but much, much more tolerable.

My productivity on that project has soared. With those three changes combined, a compile usually takes less than a minute now, so I can try over fifty times more compiles in a day. I find myself a lot more willing to experiment with the project too, now that I know I’m not going to have to wait so long for the results. All in all, it was well worth the effort.

2 Comments

Comments are closed.