Skip to content
Dragos Miloiu edited this page Aug 30, 2024 · 3 revisions

CMake Variables

CMake variables can be defined and passed to CMake during builds. For example with Zephyr, building with a shield board uses a CMake variable to include the shield of the devicetree and Kconfigs in the build. When building for the G1120B0MIPI display shield with command line, -DSHIELD=g1120b0mipi is added to the command for CMake.

CMake variables can also be added in a VS Code project with the MCUXpresso extension. After creating/importing the project, open the project file CMakePresets.json. Add the variable to the cacheVariables group. In this example, the line "SHIELD": "g1120b0mipi" was added. The CMakePresets.json file includes different build configurations, typically a Debug and Release build configs. The CMake variables should be added to each build config that are needed. The image below shows adding the SHIELD variable to the debug build config.

cmake-presets.json

Save the file, clean and build the project. Now the new CMake variable is included in the build.

It is recommended to use the CMakePresets.json file when defining any additional CMake variables, this allows you to define different variables per build configuration as needed. settings.json is another file where CMake variables may be stored. To move the CMake variables to presets simply remove them from settings.json file and add them to CMakePresets.json under cacheVariables.

cmake-settings.json cmake-presets.json-with-extra-args

You can define additional CMake variables to the CMakePresets.json file using the UI by navigating to the Build Configurations section of your project. Choose the build configuration you want to modify, then add your CMake variables in the CMake Extra Args text field.

extra-args-build-config-ui

home

Clone this wiki locally