# add-compile-options use for alternatives

**URL:** https://discourse.cmake.org/t/add-compile-options-use-for-alternatives/15337
**Category:** Usage
**Tags:** os:windows
**Created:** [November 21, 2025, 9:53pm UTC](https://discourse.cmake.org/t/add-compile-options-use-for-alternatives/15337 "2025-11-21T21:53:21Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![kfsone](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kfsone/32/1880_2.png) [@kfsone](https://discourse.cmake.org/u/kfsone)
#### Post date: [November 21, 2025, 9:53pm UTC](https://discourse.cmake.org/t/add-compile-options-use-for-alternatives/15337/1 "2025-11-21T21:53:21Z")

</div>

`add_compile_options(/Ob2)` on windows will cause msvc to complain on every file that you’re overriding the `string(APPEND CMAKE_${lang}_FLAGS_RELWITHDEBINFO_INIT "${_MD}${_Zi} /O2 /Ob1 /DNDEBUG")` in the base cmake config.

CMake seems to see `/Ob1` and `/Ob2` as unrelated flags not different values passed to the same flag (/Ob=1 vs /Ob=2)?

While this particular flavor is ms-unique it seems like other compilers have something equivalent with “–no-…” for example, or varying “-g” flags etc.

So what’s the _sane_ and portable way to do it? this feels … wrong:

`foreach (lang in C CXX)`  
` foreach (rel in RELEASE RELWITHDEBINFO)`  
` string (REGEX REPLACE “/Ob[02]?” ““ CMAKE_${lang}_FLAGS_${rel} “${CMAKE_${lang}_FLAGS_${rel}}”`  
` endforeach ()`  
`endforeach ()`  
`add_compile_options($<IF:$<OR:$<CONFIG:Release>,$<CONFIG:RelWithDebInfo>>,/Ob2,/Ob1>)`
