Prototype: Precompiled headers for Visual Studio


#1

Dear all,

Enclosed is a diff format patch for the current (as of this writing) Projucer that permits you to export Visual Studio projects that make use of precompiled headers.

A lot of people consider precompiled headers to be ugly. I don’t care about ugly. The performance benefit for precompiled headers during compilation of large projects is significant. I’m able to cut my compile times by a large percentage with precompiled headers produced by this patch.

Using precompiled headers on Visual Studio requires some setup.

To use this patch, create a precompilable hpp file, which #include’s the majority of large library headers for your project. I’ll call mine precompiled_header.hpp:

// precompiled_header.hpp
#include “windows”
#include “juce”
#include “whatever-other-slow-includes-you-want”

Now create a precompiled_header.cpp file and add it to your project. The base name of the file must match the base name of the precompiled header file. The precompiled_header.cpp file must contain exactly one line:

// precompiled_header.cpp
#include “precompiled_header.hpp”

Now include this precompiled_header.cpp file in your project.

In the Exporters section of the Projucer, select the Visual Studio exporter for the version you’re using. Select the “Use precompiled headers” checkbox, and also type in the name of the precompiled header file – in this example, precompiled_header.hpp.

Save and export your project to Visual Studio solution file, open the solution file in Visual Studio, and hopefully you should enjoy much faster compile times. In particular, you will note that precompiled_header.cpp is compiled first, and then the rest of the .cpp files are compiled much more quickly, as though precompiled_header.hpp has been appended to the top of each .cpp file.

It should be straightforward to port this patch to JUCE’s other targets. I may take a crack at MacOS soon.

Precompiled headers are not a magic bullet for slow compilation. But used judiciously, precompiled headers can help save your sanity on big projects.

patch.diff.txt (9.7 KB)


#2

If the Visual Studio settings are set properly, it is not necessary to include precompiled_header.hpp in each .cpp file. Visual Studio will pass /FI precompiled_hearder.hpp to MSVC, which includes precompiled_header.hpp automatically (see https://docs.microsoft.com/en-us/cpp/build/reference/fi-name-forced-include-file for more details). You still need precompiled_header.cpp, but it can be empty.


Using Precompiled Headers
#3

McMartin, you are correct. It was a one-line change to add your idea to this patch, so I went ahead and added it to the patch above. Others, if this doesn’t fit your requirements, you’ll see the single added line at the top of the diff.


#4

This is all good stuff, but wondering why JUCE on win and OSX doesn’t support precompiled headers out of the box? Maybe a JUCE dev can comment?


#5

@ed95 Hi Ed - can someone comment on whether this is on the road map at all? Using pch on windows/macos can drastically reduce compile times for us when not using modules.