# 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:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![mscofield0](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mscofield0/32/1413_2.png) [@mscofield0](https://discourse.cmake.org/u/mscofield0)
#### Post date: [September 29, 2021, 9:22am UTC](https://discourse.cmake.org/t/setting-cmake-cxx-flags-within-a-function/4163/1 "2021-09-29T09:22:19Z")

</div>

It seems that `CMAKE_CXX_FLAGS` isn’t being set when I set it inside of a function. I tried checking the contents, it is empty before and after, even within the same function.

These are all things I’ve tried, neither work for me:

```auto
set(${CMAKE_CXX_FLAGS} "${CMAKE_CXX_FLAGS} ${val}")

```

```auto
set(${CMAKE_CXX_FLAGS} "${${CMAKE_CXX_FLAGS}} ${val}")

```

```auto
set(${CMAKE_CXX_FLAGS} "${${CMAKE_CXX_FLAGS}} ${val}" PARENT_SCOPE)

```

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [September 29, 2021, 9:28am UTC](https://discourse.cmake.org/t/setting-cmake-cxx-flags-within-a-function/4163/2 "2021-09-29T09:28:25Z")

</div>

You are wrong. To set a variable, use just the variable name, do not expand it:

```cmake
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${val}" PARENT_SCOPE)

```

---

<div class="post-metadata">

### Author: ![mscofield0](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mscofield0/32/1413_2.png) [@mscofield0](https://discourse.cmake.org/u/mscofield0)
#### Post date: [September 29, 2021, 9:30am UTC](https://discourse.cmake.org/t/setting-cmake-cxx-flags-within-a-function/4163/3 "2021-09-29T09:30:11Z")

</div>

Huh, when is the `${VAR}` syntax used then?

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [September 29, 2021, 9:46am UTC](https://discourse.cmake.org/t/setting-cmake-cxx-flags-within-a-function/4163/4 "2021-09-29T09:46:24Z")

</div>

When you pass variable name to a function:

```cmake
function (UPDATE_MY_VAR my_var)
  set(${my_var} "my value" PARENT_SCOPE)
endfunction()

update_my_var(VAR_TO_UPDATE)
# here VAR_TO_UPDATE has value "my value"

```

---

<div class="post-metadata">

### Author: ![mscofield0](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mscofield0/32/1413_2.png) [@mscofield0](https://discourse.cmake.org/u/mscofield0)
#### Post date: [September 29, 2021, 9:47am UTC](https://discourse.cmake.org/t/setting-cmake-cxx-flags-within-a-function/4163/5 "2021-09-29T09:47:51Z")

</div>

Ah, okay. Also, is there a way of forwarding the scope of the variable above two or more functions?

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [September 29, 2021, 10:10am UTC](https://discourse.cmake.org/t/setting-cmake-cxx-flags-within-a-function/4163/6 "2021-09-29T10:10:59Z")

</div>

No. You can only update variables of the parent scope.

---

<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)

```

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [September 29, 2021, 6:08pm UTC](https://discourse.cmake.org/t/setting-cmake-cxx-flags-within-a-function/4163/8 "2021-09-29T18:08:50Z")

</div>

@ben.boeckel You mean `target_compile_options`.

---

<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, 6:33pm UTC](https://discourse.cmake.org/t/setting-cmake-cxx-flags-within-a-function/4163/9 "2021-09-29T18:33:14Z")

</div>

Indeed, thanks. I’ve updated the comment.
