UNITY_BUILD

When this property is set to true, the target source files will be combined into batches for faster compilation. This is done by creating a (set of) unity sources which #include the original sources, then compiling these unity sources instead of the originals. This is known as a Unity or Jumbo build. The UNITY_BUILD_BATCH_SIZE property controls the upper limit on how many sources can be combined per unity source file.

Unity builds are not currently supported for all languages. CMake version 3.17.5 supports combining C and CXX source files. For targets that mix source files from more than one language, CMake will separate the languages such that each generated unity source file only contains sources for a single language.

This property is initialized by the value of the CMAKE_UNITY_BUILD variable when a target is created.

Note

Projects should not directly set the UNITY_BUILD property or its associated CMAKE_UNITY_BUILD variable to true. Depending on the capabilities of the build machine and compiler used, it might or might not be appropriate to enable unity builds. Therefore, this feature should be under developer control, which would normally be through the developer choosing whether or not to set the CMAKE_UNITY_BUILD variable on the cmake(1) command line or some other equivalent method. However, it IS recommended to set the UNITY_BUILD target property to false if it is known that enabling unity builds for the target can lead to problems.

ODR (One definition rule) errors

When multiple source files are included into one source file, as is done for unity builds, it can potentially lead to ODR errors. CMake provides a number of measures to help address such problems: