# Retrieving compile definitions with genex evaluated

**URL:** https://discourse.cmake.org/t/retrieving-compile-definitions-with-genex-evaluated/9425
**Category:** Code
**Created:** [November 14, 2023, 9:54am UTC](https://discourse.cmake.org/t/retrieving-compile-definitions-with-genex-evaluated/9425 "2023-11-14T09:54:18Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![eriols](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eriols/32/945_2.png) [@eriols](https://discourse.cmake.org/u/eriols)
#### Post date: [November 14, 2023, 9:54am UTC](https://discourse.cmake.org/t/retrieving-compile-definitions-with-genex-evaluated/9425/1 "2023-11-14T09:54:18Z")

</div>

I have a build which uses the `unifdef` tool and hence is dependent on having the compile definitions for built targets available as input to this. Now that I was adding the compile definitions using genexes I’d like to learn a method to retrieve them and have the genexes evaluated. `get_target_property` gives me them unevaluated.  
Is there a canonical way (without reverting to just store all definitions in a list instead)?

---

<div class="post-metadata">

### Author: ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)
#### Post date: [November 14, 2023, 1:23pm UTC](https://discourse.cmake.org/t/retrieving-compile-definitions-with-genex-evaluated/9425/2 "2023-11-14T13:23:56Z")

</div>

By definition, generator expressions (“genexes”) are evaluated at _generate_ time (hence their name), so you cannot access their evaluated value at configure time — it may not even be fully defined yet. You can of course send unevaluated genexes into a context which will eventually evaluate them, such as using them in the arguments of a command run via `add_custom_command()`. Perhaps something like this:

```auto
add_custom_command(
  OUTPUT result.ext
  COMMAND some.exe --defines $<TARGET_PROPERTY:MyTarget,COMPILE_DEFINITIONS>
)

```

For more complex situations, you may also want to look at the genexes [`$<GENEX_EVAL>` and `$<TARGET_GENEX_EVAL>`](https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html#multi-level-expression-evaluation).

---

<div class="post-metadata">

### Author: ![eriols](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eriols/32/945_2.png) [@eriols](https://discourse.cmake.org/u/eriols)
#### Post date: [November 16, 2023, 10:21am UTC](https://discourse.cmake.org/t/retrieving-compile-definitions-with-genex-evaluated/9425/3 "2023-11-16T10:21:44Z")

</div>

Thanks, I kind of managed to get it to work for `unifdef` (which can accept a newline separated file with defines) with the following beauty:  
`file(GENERATE OUTPUT defs.txt CONTENT $<LIST:JOIN,$<LIST:TRANSFORM,$<TARGET_PROPERTY:my_tgt,INTERFACE_COMPILE_DEFINITIONS>,PREPEND,-D>,\n>) `  
But then I realized I need to pass on the definitions to Doxygen as well, which needs a space separated string. So I wonder if there is a way to have this stored in a variable instead of written to disk (and then read up again, and that is when it’s starting to get hard to justify)?

---

<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: [November 18, 2023, 9:50pm UTC](https://discourse.cmake.org/t/retrieving-compile-definitions-with-genex-evaluated/9425/4 "2023-11-18T21:50:13Z")

</div>

> [@eriols](#):
>
> So I wonder if there is a way to have this stored in a variable instead of written to disk (and then read up again, and that is when it’s starting to get hard to justify)?

Variables don’t really exist at generate time, so you can’t get the value in a variable. Instead, a `$<SPACE>` genex (which doesn’t exist yet, but an issue would help) may be useful here as the join string.

---

<div class="post-metadata">

### Author: ![eriols](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eriols/32/945_2.png) [@eriols](https://discourse.cmake.org/u/eriols)
#### Post date: [December 12, 2024, 1:45pm UTC](https://discourse.cmake.org/t/retrieving-compile-definitions-with-genex-evaluated/9425/5 "2024-12-12T13:45:32Z")

</div>

I stashed this problem away for a year but now I’m back to have my bloodpressure tested again.  
So I now tried to just read the compile definitions up from the `compile_commands.json` file. But then that file has not been created yet when I need it. I don’t know if it’s feasible to create a whole lot of dependencies on its existence when I should’t “have to”. I.e. I know the compile definitions at config time and so should cmake imo.

It feels like there could be a usecase for a config time GENEX. I.e the ones I’m setting now are all dependent on values known at this time (`option()`) and therefore I would like to be able to read them back at this time too.

And then another workaround I thought about was just storing the definitions in a list. But when creating it I really missed a ternary operator.  
Eg instead of the nasty repetition of

```auto
if(WITH_SOME_FEATURE)
      list(APPEND mydefs SOME_FEATURE=1
   else()
     list(APPEND mydefs SOME_FEATURE=0
   endif()

```

… one could do:

```auto
list(APPEND mydefs SOME_FEATURE=(WITH_SOME_FEATURE?1:0)

```

No dice eh?

---

<div class="post-metadata">

### Author: ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)
#### Post date: [December 13, 2024, 7:10pm UTC](https://discourse.cmake.org/t/retrieving-compile-definitions-with-genex-evaluated/9425/6 "2024-12-13T19:10:42Z")

</div>

After seeing you creating complicated GenExes with lists, I’m not sure if you have overseen the most simple ones: `SOME_FEATURE=$<BOOL:${WITH_SOME_FEATURE}>`

---

<div class="post-metadata">

### Author: ![eriols](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eriols/32/945_2.png) [@eriols](https://discourse.cmake.org/u/eriols)
#### Post date: [December 29, 2024, 9:40pm UTC](https://discourse.cmake.org/t/retrieving-compile-definitions-with-genex-evaluated/9425/7 "2024-12-29T21:40:07Z")

</div>

Nah, the genexes are not evaluated until build time so I get `SOME_FEATURE=$<BOOL:OFF>;` in the list.

---

<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: [February 20, 2026, 3:04am UTC](https://discourse.cmake.org/t/retrieving-compile-definitions-with-genex-evaluated/9425/8 "2026-02-20T03:04:27Z")

</div>

I think you should be able to `file(GENERATE)` the `Doxyfile` and put something like the fancy `$<GENEX>` into the contents. Might still need that `$<SPACE>` genex though.
