I Have a Need. . . a Need for Speed: Reducing Build Times

Dear Dr. GUI:

This is not a GUI question really, but I didn’t know a better place to pose it. So, what is the benefit of using precompiled header files if a compiler supports incremental linking? Or, perhaps more correctly stated, is there any point to putting any file other than a system header file in the precompiled header if a compiler can handle “patching” any change of user code incrementally? The obvious assumption I’m making is that a compiler that supports incremental compiling can incrementally update an object file (from a C or C++ file) whether that change occurred in the source file or the header file. Is this true?

Larry Hall

Dr. GUI replies:

Dr. GUI has seen a lot of confusion about this. If you take two aspirin , I’m sure you’ll feel better.

Precompiled headers, incremental linking, incremental compilation, and minimal rebuild. They all speed up builds in different ways. For the fastest builds, you should use them all. Otherwise you’ll get through the latest Dilbert cartoons on the Web before your first coffee break, and you’ll have to do real work while waiting for builds the rest of the day.

Precompiled headers save the compiler from having to parse the header (.H) files for each .CPP file in which the headers are included. It’s much faster to load the precompiled header, large as it is, into memory than to parse the 50,000+ lines of the system header files for each .CPP module you compile. Since the system headers are so large, it doesn’t usually make sense to include your project’s own headers in the precompiled header, especially if they’re changing. (One little change in a precompiled header means that the entire project has to be rebuilt from scratch, regardless of whether the sources have changed.) However, if your project has headers (such as headers for your own libraries) that don’t change, including them in your precompiled header can speed up builds somewhat.

Incremental linking allows you to make relatively simple changes to your program without having to relink the entire program. If you make major changes, however, you’ll have to do a full link. Visual C++ 4.0 can use incremental linking much more often than previous versions could.

Incremental compilation allows the build system to compile only the functions that have changed, rather than entire modules. If you have long source files, this can be a big win.

Minimal rebuild is new in Visual C++ 4.0. It examines changes you’ve made to header files, then compiles only the source files that actually depend on that particular set of changes. (Before, you had to recompile every source module that included a header that was changed, even if you changed only comments in the header.)

As you can see, the combination of these technologies can markedly reduce build times. For more information on these technologies, look them up in Visual C++ 4.0 Books Online or the MSDN Library.