# CMake Target Unity Builds

**URL:** https://discourse.cmake.org/t/cmake-target-unity-builds/6776
**Category:** Usage
**Created:** [November 2, 2022, 10:21pm UTC](https://discourse.cmake.org/t/cmake-target-unity-builds/6776 "2022-11-02T22:21:12Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Drue\_Peters](https://discourse.cmake.org/user_avatar/discourse.cmake.org/drue_peters/32/2629_2.png) [@Drue\_Peters](https://discourse.cmake.org/u/Drue_Peters)
#### Post date: [November 2, 2022, 10:21pm UTC](https://discourse.cmake.org/t/cmake-target-unity-builds/6776/1 "2022-11-02T22:21:12Z")

</div>

I’ve looked at the documentation, but it’s not obvious to me.

Our codebase has multiple libraries. A couple of them have a lot of source files and need to be updated often. Is it possible to enable a unity build just on specific targets?

So, if I have libraries A, B, and C part of this codebase. Can I have B be the only library to use a unity build?

---

<div class="post-metadata">

### Author: ![benthevining](https://discourse.cmake.org/user_avatar/discourse.cmake.org/benthevining/32/1924_2.png) [@benthevining](https://discourse.cmake.org/u/benthevining)
#### Post date: [November 2, 2022, 10:29pm UTC](https://discourse.cmake.org/t/cmake-target-unity-builds/6776/2 "2022-11-02T22:29:50Z")

</div>

There’s a target property for that, I believe it’s called `UNITY_BUILD`.

---

<div class="post-metadata">

### Author: ![Drue\_Peters](https://discourse.cmake.org/user_avatar/discourse.cmake.org/drue_peters/32/2629_2.png) [@Drue\_Peters](https://discourse.cmake.org/u/Drue_Peters)
#### Post date: [November 2, 2022, 10:49pm UTC](https://discourse.cmake.org/t/cmake-target-unity-builds/6776/3 "2022-11-02T22:49:13Z")

</div>

> [@benthevining](#):
>
> `UNITY_BUILD`

I think I might misunderstand how that property works. The documentation says to disable it if you want to disable unity builds even when a developer passes the unity option.

But, by default does CMake only perform a unity build based on targets?  
For some reason, I had in my head it might combine the source for multiple targets. However, while typing this out I realize that decision wouldn’t make much sense at all.

---

<div class="post-metadata">

### Author: ![benthevining](https://discourse.cmake.org/user_avatar/discourse.cmake.org/benthevining/32/1924_2.png) [@benthevining](https://discourse.cmake.org/u/benthevining)
#### Post date: [November 2, 2022, 11:05pm UTC](https://discourse.cmake.org/t/cmake-target-unity-builds/6776/4 "2022-11-02T23:05:04Z")

</div>

I think you can also use that property to enable unity build for only some targets… But even if not, you could achieve the same effect by explicitly setting the UNITY\_BUILD property to false for the targets you don’t want to build as unity.

---

<div class="post-metadata">

### Author: ![Drue\_Peters](https://discourse.cmake.org/user_avatar/discourse.cmake.org/drue_peters/32/2629_2.png) [@Drue\_Peters](https://discourse.cmake.org/u/Drue_Peters)
#### Post date: [November 3, 2022, 11:38pm UTC](https://discourse.cmake.org/t/cmake-target-unity-builds/6776/5 "2022-11-03T23:38:17Z")

</div>

Tried it. Got stuff to fix in our codebase first. There are a couple specific targets I wanted to try this on, but they are the ones that fail. I’m going to book mark this though. Thanks for the info.

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [November 4, 2022, 4:20pm UTC](https://discourse.cmake.org/t/cmake-target-unity-builds/6776/6 "2022-11-04T16:20:56Z")

</div>

you may simply add this line for the target you want to build this way:

```cmake
set_target_properties(my_target PROPERTIES UNITY_BUILD ON)

```
