# Setting \`CMAKE\_CXX\_FLAGS\` within a function

**URL:** https://discourse.cmake.org/t/setting-cmake-cxx-flags-within-a-function/4163
**Category:** Usage
**Created:** [September 29, 2021, 9:22am UTC](https://discourse.cmake.org/t/setting-cmake-cxx-flags-within-a-function/4163 "2021-09-29T09:22:19Z")
**Posts on this page:** 1
**Showing post:** 7

<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: [September 29, 2021, 5:50pm UTC](https://discourse.cmake.org/t/setting-cmake-cxx-flags-within-a-function/4163/7 "2021-09-29T17:50:21Z")

</div>

Note that the better solution these days is something like:

```cmake
add_library(buildflags INTERFACE)
target_compile_options(buildflags INTERFACE -Wsome-warning)

# For each target
target_link_libraries(mytgt PRIVATE buildflags)

```

---

_[View the full topic](https://discourse.cmake.org/t/setting-cmake-cxx-flags-within-a-function/4163)._
