Speeding up C++ compile times

One downside of the new control system was the C++ toolchain change, which has much slower compile times than the previous tooling. It’s at the point where we are considering switching to Java.

Are there tweaks that teams are using to improve compile times?

The toolchain is configured a little differently but it is the same compiler. Both Wind River Workbench (a feature rich custom Eclipse setup) and our new plain Eclipse setup use a cross version of gcc. So if you are seeing differences, it is most likely related to changes in CPU throughput, lower memory resources etc.

No way is it slower than Java.


Are you compiling using the -j flag?

-j enables multithreaded builds.

Edit: oops. Depends on what IDE you’re using. The -j flag works from command line. If building within eclipse, right the project, click properties, C/C++ build, behavior, enable parallel build.

Basically what @kiettyyyy said. Enabling parallel builds is a very simple way to drastically improve compile times. This has very good scaling as you go up in cores. So a high core count processor like an Intel i7-5820k where you have 12 logical cores (6 physical + 6 virtual) enables you to compile 12 different files of a project at the same time.

I use this method in my personal projects along with a 28 core 56 thread compiling/rendering server. It breezes through my game engine which is approximately 900000 lines of code so its still a baby engine but it takes quite some time to compile in single threaded mode.

Another way to speed up C++ compile times significantly is to set up compilation with precompiled headers. The way it basically works is you set up a new compilation unit designated as your precompiled header that will be used as a compilation checkpoint for all your other compilation units (typically .cpp files).

If you are unfamiliar with C++ compilation, for every compilation unit, the compiler needs to reparse every single header included throughout the include chain of that unit. If your system library headers are extensive (like for example windows.h can include hundreds of other headers) this can make compiling each unit take a long time. By including commonly included but infrequently changed headers into your precompiled header unit, you are giving the compiler a checkpoint at which to begin compilation for each other compilation unit so it doesn’t have to reparse those headers that were included in your precompiled header.

If you do have to change any of the headers included by your precompiled header, the entire project will need to be rebuilt. This is why you only want to include infrequently changed headers.

You’ll have to look up how to do this with the FRC C++ toolchain as I’ve only set it up with MSVC/VS, but the general process is the same.
-Add a new .cpp that all it does is include a new .hpp.
-In your new .hpp, include any headers that are commonly included (WPILib/STL headers are probably all you want to include).
-Designate your new .cpp as the precompiled header compilation unit.
-Include your new .h at the very top of all your existing .cpp files.