# How to remove compile flags

**URL:** https://discourse.cmake.org/t/how-to-remove-compile-flags/6693
**Category:** Code
**Created:** [October 19, 2022, 10:13am UTC](https://discourse.cmake.org/t/how-to-remove-compile-flags/6693 "2022-10-19T10:13:07Z")
**Posts on this page:** 1
**Showing post:** 3

<div class="post-metadata">

### Author: ![tom](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/t/3d9bf3/32.png) [@tom](https://discourse.cmake.org/u/tom)
#### Post date: [October 21, 2022, 10:23am UTC](https://discourse.cmake.org/t/how-to-remove-compile-flags/6693/3 "2022-10-21T10:23:42Z")

</div>

I’m not sure how well whitelisting flags like that will work for me. If I’ve understood correctly, the process for removing a flag “foo” from the “frobnicator” target would be something like this:

1. Remove foo from the flags that are applied to all targets (CMAKE\_CXX\_FLAGS at the moment, or the myflags INTERFACE in your example),
2. Add the above generator expression for the foo flag,
3. Add the needs\_foo property to every target apart from frobnicator.

The trouble is my code base is very large, so step (3) would involve adding a property to thousands of targets. If we then ever wanted to re-instate the foo flag, we’d have to remove that property from thousands of targets.

Is there no blacklisting solution available? I’ve got a vague idea that we could write a function a bit like this, but it seems inelegant… I’m also not sure that CMAKE\_CXX\_FLAGS would actually populate those target properties.

```auto
function(remove_compiler_flag target flag)
  foreach(property IN COMPILE_DEFINITIONS COMPILE_FEATURES COMPILE_FLAGS COMPILE_OPTIONS etc)
    # Get the target property
    # If the specified flag is in that list, remove it and re-set the target property
  endforeach()
endfunction()

```

---

_[View the full topic](https://discourse.cmake.org/t/how-to-remove-compile-flags/6693)._
