Blog 4 min read

The Importance of C++ Distributed Build Systems

Share this article
The Importance of C++ Distributed Build Systems

As a C++ developer, if you have ever built a C# or Java project, you may be surprised by how much faster the build process can be compared with C++. For some C++ projects, a build may take only a few minutes, while for others it can take several hours, depending on the size of the project. Even when the compilation phase is parallelized across all available cores, C++ builds can still take much longer than builds in other languages. Here are some of the reasons, as explained in this useful Stack Overflow answer:

  • Header files: Every single compilation unit requires hundreds or even thousands of headers to be 1: loaded, and 2: compiled. Every one of them typically has to be recompiled for every compilation unit, because the preprocessor ensures that the result of compiling a header might vary between every compilation unit. (A macro may be defined in one compilation unit which changes the content of the header).This is probably the main reason, as it requires huge amounts of code to be compiled for every compilation unit, and additionally, every header has to be compiled multiple times (once for every compilation unit that includes it)
  • Linking: Once compiled, all the object files have to be linked together. This is basically a monolithic process that can't very well be parallelized and has to process your entire project.
  • Parsing: The syntax is extremely complicated to parse, depends heavily on context, and is very hard to disambiguate. This takes a lot of time
  • Templates: In C#, List<T> is the only type that is compiled, no matter how many instantiations of List you have in your program. In C++, vector<int> is a completely separate type from vector<float>, and each one will have to be compiled separately.

What happens if the build process is slow?

If a build takes 10 minutes, the company loses the equivalent of 10 minutes multiplied by the number of builds performed each day. The total cost also depends on the number of developers. The more developers there are, the more builds are performed, and the greater the productivity loss becomes.

The IncrediBuild ROI calculator provides an estimate of how much time is lost when a team has X developers and each build takes Y minutes. However, this calculator is optimistic because it can’t count the hidden cost. For example, if a developer starts a build and has to wait 10 minutes, they may switch to another activity, such as browsing the web, and become distracted. As a result, a 10-minute interruption can easily turn into 15 or 20 minutes.

A fast build helps developers stay focused on the code they are working on.

How can we optimize the build process of C++ projects? Several techniques can help optimize the build process; some of them are listed here

  • Pimpl Idiom
  • Forward Declarations
  • Guard Conditions
  • Reduce interdependency
  • And, of course, one of the most widely used techniques: precompiled headers

However, using the techniques above has some drawbacks:

  • It’s difficult to ensure that these solutions are really applied; a code review is needed regularly.
  • Even when these solutions are applied correctly, the time savings may be limited, and builds can still take several hours for very large projects.
  • Changing the code solely to address build-time issues is generally a bad idea; the code should be designed and implemented independently of the build process being used.

C++ Distributed Build Systems to the Rescue

A distributed build system is a program that distributes code compilation across several machines on a network. It should always generate the same results as a local compile.

Several interesting C++ distributed build systems are available. Personally, I have only used IncrediBuild.

IncrediBuild is a suite of grid computing software developed by IncrediBuild Ltd. IncrediBuild is designed to help accelerate computationally-intensive tasks by distributing them over the network, with notable applications including compiling source code, building software generally, and other software development-related tasks. Jobs can be distributed to several computers over a network, giving both the possibility of accelerating the work by using more resources than were available on the initiating computer alone.

IncrediBuild helped us significantly reduce build times and spend more time on productive tasks.

If your C++ build process takes too long, consider using a distributed build system. Explore the available tools and choose the one that best meets your needs.

Share this article