# Create list variables in CMakePresets.json

**URL:** <https://discourse.cmake.org/t/create-list-variables-in-cmakepresets-json/9861>\
**Category:** Code\
**Created:** [January 15, 2024, 4:50pm UTC](https://discourse.cmake.org/t/create-list-variables-in-cmakepresets-json/9861 "2024-01-15T16:50:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sharadhr](https://discourse.cmake.org/user_avatar/discourse.cmake.org/sharadhr/32/3528_2.png) [@sharadhr](https://discourse.cmake.org/u/sharadhr)\
**Post date:** [January 15, 2024, 4:50pm UTC](https://discourse.cmake.org/t/create-list-variables-in-cmakepresets-json/9861/1 "2024-01-15T16:50:00Z")

</div>

Is there a way to create list variables in `CMakePresets.json`? For instance, a list of preprocessor macros to then be passed to `target_compile_definitions`?

In CMake syntax, I’d do something like

```cmake
set(MY_DEFINES_1
  MACRO_1 
  MACRO_2=value1
  MACRO_3=value2
)
# which is equivalent to...
list(MY_DEFINES_2 MACRO1;MACRO_2=value1;MACRO_3=value2)

# and either may be used like
add_executable(main main.cpp)
target_compile_definitions(main
  PRIVATE ${MY_DEFINES}
)
# which causes something like g++ -DMACRO_1 -DMACRO_2=value1 -DMACRO_3=value2 main.cpp`

```

Now, I have a project that requires at least two dozen macros, and I’d like to define these as early as possible—in `CMakePresets.json` as part of an inheritable, hidden configure preset, under `cacheVariables`. Intuitively, I would think of using JSON arrays:

```json
configurePresets: [
...
  {
    "name": "defines",
    "hidden": true,
    "cacheVariables": {
      "MY_DEFINES": [
        "MACRO_1",
        "MACRO_2=value1",
        "MACRO_3=value2"
      ]
    }
  },
  {
    "name": "windows-debug",
    "inherits": [
      "defines"
    ]
  }
...
]

```

I foresee these could be used for command-line flags (`CMAKE_CXX_FLAGS`), macros, and other house-keeping lists to keep as clean a set of CMakeLists files as possible. Is this legitmate? I know other projects like [`cmake-init`](https://github.com/friendlyanon/cmake-init) simply dump [a whitespace-separated list of strings](https://github.com/friendlyanon/cmake-init-executable/blob/d7abd085c3f5af1ca5d583e48433634f0b35ed8b/CMakePresets.json#L61) (actually just one string) into these variables, and it makes for extremely long lines and rather unpleasant reading.

I also welcome feedback on best practices. Is this the best idea, setting macros and flags in presets? Should I be defining these variables in a `MyMacros.cmake` file instead, to be `include()`d in my various `CMakeLists.txt`s?

Thanks.

* * *

**EDIT** : I notice [this issue](https://gitlab.kitware.com/cmake/cmake/-/issues/22523) is still open. It’s still not clear how I can currently define list variables—do I just set `"MY_DEFINES": "MACRO1;MACRO_2=value1;MACRO_3=value2"` in the presets file?

---

<div class="post-metadata">

**Author:** ![int\_main](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/i/e95f7d/32.png) [@int\_main](https://discourse.cmake.org/u/int_main)\
**Post date:** [January 31, 2025, 1:32pm UTC](https://discourse.cmake.org/t/create-list-variables-in-cmakepresets-json/9861/2 "2025-01-31T13:32:11Z")

</div>

> [@sharadhr](#):
>
> do I just set `"MY_DEFINES": "MACRO1;MACRO_2=value1;MACRO_3=value2"` in the presets file?

yes
