# \`COMPILE\_DEFINITIONS\` with generator expression: "$\<${gcc\_like}:-Werror\>" "$\<${msvc}:-WX\>"

**URL:** https://discourse.cmake.org/t/compile-definitions-with-generator-expression-gcc-like-werror-msvc-wx/8254
**Category:** Code
**Created:** [June 4, 2023, 4:34pm UTC](https://discourse.cmake.org/t/compile-definitions-with-generator-expression-gcc-like-werror-msvc-wx/8254 "2023-06-04T16:34:26Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![SamuelMarks](https://discourse.cmake.org/user_avatar/discourse.cmake.org/samuelmarks/32/1619_2.png) [@SamuelMarks](https://discourse.cmake.org/u/SamuelMarks)
#### Post date: [June 4, 2023, 4:34pm UTC](https://discourse.cmake.org/t/compile-definitions-with-generator-expression-gcc-like-werror-msvc-wx/8254/1 "2023-06-04T16:34:26Z")

</div>

Attempts:

```cmake
set(gcc_like "$<COMPILE_LANG_AND_ID:C,CXX,ARMClang,AppleClang,Clang,GNU,LCC>")
set(msvc "$<COMPILE_LANG_AND_ID:C,CXX,MSVC>")

try_compile(
        FALL_THROUGH_SUPPORTED
        ${CMAKE_BINARY_DIR}
        SOURCES "${CMAKE_CURRENT_LIST_DIR}/tests/features/fallthrough.c"
        COMPILE_DEFINITIONS "$<${gcc_like}:-Werror>" "$<${msvc}:-WX>"
)

```

```cmake
COMPILE_DEFINITIONS "$$<${gcc_like}:-Werror>" "$$<${msvc}:-WX>"

```

```cmake
COMPILE_DEFINITIONS
  "$<${gcc_like}:$<BUILD_INTERFACE:-Werror>>"
  "$<${msvc}:$<BUILD_INTERFACE:-WX>>"
)

```

All gave this error:

```auto
win/x64/ninja.exe' '-C' 's2n-tls/cmake-build-debug/CMakeFiles/CMakeTmp' '-t' 'recompact'

  failed with:

   ninja: error: build.ninja:53: bad $-escape (literal $ must be written as $$)
    FLAGS = /DWIN32 /D_WINDOWS /W3 /MDd /Zi /Ob0 /Od /RTC1 $$<$<COMPILE...
                                                                 ^ near here

```

From [https://cmake.org/cmake/help/latest/prop\_tgt/COMPILE\_DEFINITIONS.html](https://cmake.org/cmake/help/latest/prop_tgt/COMPILE_DEFINITIONS.html) you do support generator expressions here. What’s the right syntax?

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [June 4, 2023, 11:17pm UTC](https://discourse.cmake.org/t/compile-definitions-with-generator-expression-gcc-like-werror-msvc-wx/8254/2 "2023-06-04T23:17:23Z")

</div>

The `$<COMPILE_LANG_AND_ID:...>` generator expression only takes _one_ language and multiple compiler IDs. You’re trying to use multiple languages, so your second and later languages would get treated as compiler IDs.

But your main problem is that the `try_compile()` command doesn’t support generator expressions. The general rule is that if the documentation of a command or property doesn’t explicitly say it supports generator expressions, then it doesn’t support them.

---

<div class="post-metadata">

### Author: ![SamuelMarks](https://discourse.cmake.org/user_avatar/discourse.cmake.org/samuelmarks/32/1619_2.png) [@SamuelMarks](https://discourse.cmake.org/u/SamuelMarks)
#### Post date: [June 5, 2023, 4:25pm UTC](https://discourse.cmake.org/t/compile-definitions-with-generator-expression-gcc-like-werror-msvc-wx/8254/3 "2023-06-05T16:25:17Z")

</div>

@craig.scott But it does say:

> Contents of `COMPILE_DEFINITIONS` may use “generator expressions” with the syntax `$<...>`. See the [`cmake-generator-expressions(7)`](https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html#manual:cmake-generator-expressions(7)) manual for available expressions. See the [`cmake-buildsystem(7)`](https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#manual:cmake-buildsystem(7)) manual for more on defining buildsystem properties.

[https://cmake.org/cmake/help/latest/prop\_tgt/COMPILE\_DEFINITIONS.html](https://cmake.org/cmake/help/latest/prop_tgt/COMPILE_DEFINITIONS.html)

Ok so `try_compile` uses different `COMPILE_DEFINITIONS` than that one?

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [June 5, 2023, 10:38pm UTC](https://discourse.cmake.org/t/compile-definitions-with-generator-expression-gcc-like-werror-msvc-wx/8254/4 "2023-06-05T22:38:52Z")

</div>

The `try_compile()` command creates a separate sub-build to do the try-compile. You’re expecting details from the main project to be available in that sub-build, but that isn’t possible. Generator expressions are evaluated at generation time _of the main project_. The `try_compile()` command runs during the CMake _configuration_ stage, which is before the generation stage. Thus, the `try_compile()` command cannot evaluation generator expressions. It may try to pass them down in their original form to the sub-build, but it won’t be expecting any because that command’s documentation doesn’t say that it supports generator expressions.

So while the `COMPILE_DEFINITIONS` property does support generator expressions, the `try_compile()` command you’re trying to thread them through via does not. And there’s no way they can be supported in this case (for the way you want to use them) because the information you’re expecting them to provide isn’t available yet at the point of the `try_compile()` call.

---

<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: [June 7, 2023, 11:51am UTC](https://discourse.cmake.org/t/compile-definitions-with-generator-expression-gcc-like-werror-msvc-wx/8254/5 "2023-06-07T11:51:31Z")

</div>

> [@craig.scott](#):
>
> So while the `COMPILE_DEFINITIONS` property does support generator expressions, the `try_compile()` command you’re trying to thread them through via does not. And there’s no way they can be supported in this case (for the way you want to use them) because the information you’re expecting them to provide isn’t available yet at the point of the `try_compile()` call.

They can be handled during the `try_compile` project’s generation time (assuming the setup has enough context to answer them reliably; for this, it should). However, it looks like there’s some escaping woes because it does try to pass things, but `ninja` chokes on `$` escaping. See [Issue #16395](https://gitlab.kitware.com/cmake/cmake/-/issues/16395).

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [June 7, 2023, 1:13pm UTC](https://discourse.cmake.org/t/compile-definitions-with-generator-expression-gcc-like-werror-msvc-wx/8254/6 "2023-06-07T13:13:13Z")

</div>

> [@craig.scott](#):
>
> And there’s no way they can be supported in this case (for the way you want to use them) because the information you’re expecting them to provide isn’t available yet at the point of the `try_compile()` call.

I was thinking your generator expressions were referencing target properties when I made the above comment. But I see now you are only referring to details about the toolchain. Maybe the sub-build could resolve those if the generator expressions could survive the trip through the `ninja.build` file (which Ben’s last comment seems to indicate might not be possible, at least right now).
