# How to override compiler optimisation level?

**URL:** https://discourse.cmake.org/t/how-to-override-compiler-optimisation-level/5153
**Category:** Usage
**Created:** [March 3, 2022, 4:12pm UTC](https://discourse.cmake.org/t/how-to-override-compiler-optimisation-level/5153 "2022-03-03T16:12:13Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![DavidA](https://discourse.cmake.org/user_avatar/discourse.cmake.org/davida/32/128_2.png) [@DavidA](https://discourse.cmake.org/u/DavidA)
#### Post date: [March 3, 2022, 4:12pm UTC](https://discourse.cmake.org/t/how-to-override-compiler-optimisation-level/5153/1 "2022-03-03T16:12:13Z")

</div>

What is the modern CMake way of overriding (replacing) the C++ optimisation level (for all targets) ?

For Release builds using armclang I want to replace -O3 with -Ofast.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [March 3, 2022, 6:18pm UTC](https://discourse.cmake.org/t/how-to-override-compiler-optimisation-level/5153/2 "2022-03-03T18:18:56Z")

</div>

CMake doesn’t provide an abstraction for optimization flags. Replacing or appending to `CMAKE_<LANG>_FLAGS_RELEASE` is the best available today.

---

<div class="post-metadata">

### Author: ![DavidA](https://discourse.cmake.org/user_avatar/discourse.cmake.org/davida/32/128_2.png) [@DavidA](https://discourse.cmake.org/u/DavidA)
#### Post date: [March 3, 2022, 6:24pm UTC](https://discourse.cmake.org/t/how-to-override-compiler-optimisation-level/5153/3 "2022-03-03T18:24:31Z")

</div>

Thanks. Can I do a string replace on `CMAKE_<LANG>_FLAGS_RELEASE`?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [March 3, 2022, 6:25pm UTC](https://discourse.cmake.org/t/how-to-override-compiler-optimisation-level/5153/4 "2022-03-03T18:25:36Z")

</div>

Yes, that will work (unless the user changes it themselves). I think it’d be better to just `string(APPEND)` to it since most compilers use whatever the last `-O` flag is as the “winner” (some with warnings though).

---

<div class="post-metadata">

### Author: ![DavidA](https://discourse.cmake.org/user_avatar/discourse.cmake.org/davida/32/128_2.png) [@DavidA](https://discourse.cmake.org/u/DavidA)
#### Post date: [March 3, 2022, 6:26pm UTC](https://discourse.cmake.org/t/how-to-override-compiler-optimisation-level/5153/5 "2022-03-03T18:26:32Z")

</div>

Ok, thanks.

---

<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: [March 4, 2022, 12:17am UTC](https://discourse.cmake.org/t/how-to-override-compiler-optimisation-level/5153/6 "2022-03-04T00:17:59Z")

</div>

I have a [common repository of shared CMake modules](https://github.com/benthevining/Oranges/blob/main/modules/general/LemonsDefaultTarget.cmake) that many projects use, and one of these modules defines a “default target” as an interface library. Then, any target you create can just link against this default target.

Of course, this doesn’t help if you’re wanting to mess with options for targets in projects you don’t control.
