{"id":8853,"date":"2024-04-28T16:11:07","date_gmt":"2024-04-28T23:11:07","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=8853"},"modified":"2024-04-28T16:11:07","modified_gmt":"2024-04-28T23:11:07","slug":"cmake-presets","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/documentation\/cmake\/presets\/","title":{"rendered":"CMake Presets"},"content":{"rendered":"<p>This page describes CMake presets and how they can be used with VisualGDB projects.<\/p>\n<h2>Contents<\/h2>\n<p><a href=\"#overview\">Overview<\/a><br \/>\n<a href=\"#format\">Internal Format<\/a><br \/>\n<a href=\"#editing\">Editing Presets<\/a><br \/>\n<a href=\"#tweaking\">Tweaking<\/a><br \/>\n<a href=\"#importing\">Importing Existing Projects<\/a><\/p>\n<h2><a id=\"overview\"><\/a>Overview<\/h2>\n<p>CMake Presets are essentially instructions how to build CMake projects stored in a consistent IDE-independent way. A project using presets can be opened and built using any preset-capable IDE with exactly the same results: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/interop.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8854\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/interop.png\" alt=\"\" width=\"1135\" height=\"429\" \/><\/a><\/p>\n<p>Presets store parameters that would be otherwise passed to CMake via command line: toolchain to use, target system type, debug\/release configuration type, etc.<\/p>\n<h2><a id=\"format\"><\/a>Internal Format<\/h2>\n<p>Presets are stored in the <a href=\"https:\/\/cmake.org\/cmake\/help\/latest\/manual\/cmake-presets.7.html\"><strong>CMakePresets.json<\/strong><\/a> file in the project directory. A simplified example of this file is provided below:<\/p>\n<pre>{\r\n    \"configurePresets\": [\r\n        {\r\n            \"name\": \"RaspberryPi\",\r\n            \"environment\": {\r\n                \"TOOLCHAIN_ROOT\": \"C:\/SysGCC\/raspberry\",\r\n            },\r\n            \"cacheVariables\": {\r\n                \"CMAKE_C_COMPILER\": \"$env{TOOLCHAIN_ROOT}\/bin\/arm-linux-gnueabihf-gcc.exe\",\r\n            },\r\n            \"binaryDir\": \"${sourceDir}\/build\/${presetName}\",\r\n            \"generator\": \"Ninja\",\r\n            \"hidden\": true\r\n        }\r\n    ]\r\n}<\/pre>\n<p>Instead of manually setting <strong>TOOLCHAIN_ROOT<\/strong> via the environment and passing <strong>-DCMAKE_C_COMPILER=[&#8230;]<\/strong> to CMake, you can simply run CMake as follows:<\/p>\n<pre>cmake.exe --preset=RaspberryPi\r\ncmake.exe --build build\/RaspberryPi<\/pre>\n<h2><a id=\"editing\"><\/a>Editing Presets<\/h2>\n<p>VisualGDB automatically translates CMake presets into solution configurations (replacing the normal Debug\/Release) and provides GUI for managing them. You can switch between the classic configurations and CMake presets via the <strong>VisualGDB Project Properties -&gt; CMake Build Settings -&gt; Store build settings in<\/strong> setting <strong>(1)<\/strong>:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/01-switch.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8856\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/01-switch.png\" alt=\"\" width=\"1092\" height=\"806\" \/><\/a>If you have enabled preset-based mode (you would need to click &#8216;Apply&#8217; <strong>(2)<\/strong>), the <strong>Manage<\/strong> button <strong>(3)<\/strong> in the top right corner of the window will open the preset editor:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/02-presets.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8857\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/02-presets.png\" alt=\"\" width=\"1147\" height=\"814\" \/><\/a>The editor allows creating <strong>(1)<\/strong> and deleting <strong>(2)<\/strong> presets, as well as editing settings within each preset.<\/p>\n<p>Note that the presets can be inherited. E.g. it could be convenient to define a base <strong>RaspberryPi<\/strong> preset containing all toolchain settings, and then derive separate debug\/release presets from it:<\/p>\n<pre>\u00a0{\r\n    \"name\": \"RaspberryPi-Debug\",\r\n    \"inherits\": \"RaspberryPi\",\r\n    \"cacheVariables\": {\r\n        \"CMAKE_BUILD_TYPE\": \"Debug\"\r\n    }\r\n},\r\n{\r\n    \"name\": \"RaspberryPi-Release\",\r\n    \"inherits\": \"RaspberryPi\",\r\n    \"cacheVariables\": {\r\n        \"CMAKE_BUILD_TYPE\": \"RelWithDebInfo\"\r\n    }\r\n}<\/pre>\n<p>VisualGDB fully supports preset inheritance, allowing you to select one or more base presets <strong>(3)<\/strong>. The inherited settings for each preset are initially grayed out, but can be easily copied into the currently edited preset and edited using the edit button <strong>(5)<\/strong>. Overridden settings can be reverted back to the inherited value via the revert button <strong>(4)<\/strong>.<\/p>\n<p>You can create new presets completely from scratch, inherit other presets, or let VisualGDB pre-fill the new preset with common values:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/03-new.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8858\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/03-new.png\" alt=\"\" width=\"353\" height=\"224\" \/><\/a>Selecting the &#8220;<strong>reference current toolchain<\/strong>&#8221; option <strong>(1)<\/strong> when creating a preset will automatically set variables like <strong>TOOLCHAIN_ROOT<\/strong>, <strong>CMAKE_SYSROOT<\/strong>, <strong>CMAKE_C_COMPILER<\/strong>, etc. based on the current toolchain. This is convenient when converting existing CMake projects to the preset model.<\/p>\n<p>The &#8220;<strong>create a pair of debug\/release presets<\/strong>&#8221; checkbox <strong>(2)<\/strong> will automatically mark the newly created preset as hidden and will create <strong>Debug<\/strong> and <strong>RelWithDebInfo<\/strong> presets inheriting it. This way, the actual toolchain settings will be shared between the debug and release presets instead of being duplicated between them.<\/p>\n<h2><a id=\"tweaking\"><\/a>Tweaking<\/h2>\n<p>VisualGDB only displays the most commonly used preset options. If you would like to set an option that is not shown in the list, you can manually edit <strong>CMakePresets.json<\/strong> file (VisualGDB will show the manually added setting) or edit the preset schema in the <strong>C:\\Program Files (x86)\\Sysprogs\\VisualGDB\\Rules\\CMakeData.xml<\/strong> file. The schema file simply lists all displayed properties for each preset type. E.g. the following element defines the &#8220;Installation directory&#8221; property for configuration presets:<\/p>\n<pre>&lt;PresetEntry xsi:type=\"String\"&gt;\r\n    &lt;Key&gt;installDir&lt;\/Key&gt;\r\n    &lt;Name&gt;Installation Directory&lt;\/Name&gt;\r\n&lt;\/PresetEntry&gt;<\/pre>\n<h2><a id=\"importing\"><\/a>Importing Existing Projects<\/h2>\n<p>If you already have a project that uses CMake presets, VisualGDB will automatically detect it, suggesting to reuse existing presets: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/09-presets.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8859\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2024\/04\/09-presets.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a>As long as this option is enabled during importing, VisualGDB will immediately map existing presets to solution configurations without the need to change anything in VisualGDB Project Properties.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This page describes CMake presets and how they can be used with VisualGDB projects. Contents Overview Internal Format Editing Presets<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[227],"tags":[77],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/8853"}],"collection":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/comments?post=8853"}],"version-history":[{"count":2,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/8853\/revisions"}],"predecessor-version":[{"id":8860,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/8853\/revisions\/8860"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=8853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=8853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=8853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}