# Unity build

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Unity_build
> Markdown URL: https://mediated.wiki/source/Unity_build.md
> Source: https://en.wikipedia.org/wiki/Unity_build
> Source revision: 1334539862
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

Technique to speed up C/C++ project builds

This article is about the compilation technique. For versions of the Unity game engine, see [Unity (game engine) § History](/source/Unity_(game_engine)#History).

In [software engineering](/source/Software_engineering), a **unity build** (also known as **unified build**, **jumbo build** or **blob build**) is a method used in [C](/source/C_(programming_language)) and [C++](/source/C%2B%2B) [software development](/source/Software_development) to speed up the compilation of projects by combining multiple [translation units](/source/Translation_unit_(programming)) into a single one, usually achieved by using [include directives](/source/Include_directive) to bundle multiple source files into one larger file.

## Implementation

If two different translation units FileA.cpp:

#include "Header.hpp"

// content of source file A ...

and FileB.cpp:

#include "Header.hpp"

// content of source file B ...

in the same project both include the header Header.hpp, that header will be processed twice by the compiler chain, once for each build task. If the two translation units are merged into a single source file JumboFile.cpp:

#include "FileA.cpp"
#include "FileB.cpp"

then Header.hpp will be processed only once (thanks to [include guards](/source/Include_guard)) when compiling JumboFile.cpp.[1]

## Effects

The main benefit of unity builds is a reduction of duplicated effort in parsing and compiling the content of headers that are included in more than one source file. The content of headers usually accounts for the majority of code in a source file after [preprocessing](/source/C_preprocessor). Unity builds also mitigate the overhead caused by having a large number of small source files by reducing the number of object files created and processed by the compilation chain, and allows interprocedural analysis and optimisation across the files that form the unity build task (similar to the effects of [link-time optimisation](/source/Interprocedural_optimization)). They make it also easier to detect violations of the [One Definition Rule](/source/One_Definition_Rule), because if a symbol is defined twice in different source files in the same unity build, the compiler will be able to identify the redefinition and emit a warning or error.

One of the drawbacks of unity builds is a larger [memory footprint](/source/Memory_footprint) due to larger translation units. Larger translation units can also negatively affect parallel builds, since a small number of large compile jobs is generally harder or impossible to schedule to saturate all available [parallel computing](/source/Parallel_computing) resources effectively. Unity builds can also deny part of the benefits of incremental builds, that rely on rebuilding as little code as possible, i.e. only the translation units affected by changes since the last build. These disadvantages can be offset by a great increase in the speed of the application linker stage, which no longer has to load and eliminate duplicate template codegen blocks in each translation unit. The linker stage memory use will also decrease.

Unity builds have also potentially dangerous effects on the [semantics](/source/Semantics_(computer_science)) of programs. Some valid C++ constructs that rely on internal linkage may fail under a unity build, for instance clashes of static symbols and symbols defined in anonymous namespaces with the same identifier in different files. If different C++ files define different functions with the same name, the compiler may unexpectedly resolve the [overloading](/source/Function_overloading) by selecting the wrong function, in a way that was not possible when designing the software with the files as different translation units. Another adverse effect is the possible leakage of [macro](/source/Macro_(computer_science)) definitions across different source files.[2]

## Build system support

Some build systems provide built-in support for automated unity builds, including [Visual Studio](/source/Visual_Studio),[3] [Meson](/source/Meson_(software)),[4] [CMake](/source/CMake).[5] and xmake.

## References

1. **[^](#cite_ref-1)** Kubota et al. (2019)

1. **[^](#cite_ref-2)** Kirilov, Viktor (7 July 2018). ["A guide to unity builds"](https://web.archive.org/web/20201112022452/https://onqtam.com/programming/2018-07-07-unity-builds/). Archived from [the original](https://onqtam.com/programming/2018-07-07-unity-builds/) on 2020-11-12.

1. **[^](#cite_ref-3)** Olga Arkhipova (2 July 2018). ["Support for Unity (Jumbo) Files in Visual Studio 2017 15.8 (Experimental)"](https://devblogs.microsoft.com/cppblog/support-for-unity-jumbo-files-in-visual-studio-2017-15-8-experimental/). Microsoft.

1. **[^](#cite_ref-4)** ["Unity builds"](https://mesonbuild.com/Unity-builds.html).

1. **[^](#cite_ref-5)** ["UNITY_BUILD - CMake 3.17.0 Documentation"](https://cmake.org/cmake/help/latest/prop_tgt/UNITY_BUILD.html).

- Kubota, Takafumi; Yusuke, Suzuki; and, Kenji Kono (2019). *To unify or not to unify: a case study on unified builds (in WebKit)*. Proceedings of the 28th International Conference on Compiler Construction. [doi](/source/Doi_(identifier)):[10.1145/3302516.3307347](https://doi.org/10.1145%2F3302516.3307347).

---
Adapted from the Wikipedia article [Unity build](https://en.wikipedia.org/wiki/Unity_build) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Unity_build?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
