{"id":27911,"date":"2021-04-20T16:30:20","date_gmt":"2021-04-20T16:30:20","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cppblog\/?p=27911"},"modified":"2021-11-09T18:34:28","modified_gmt":"2021-11-09T18:34:28","slug":"cmake-presets-integration-in-visual-studio-and-visual-studio-code","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/cmake-presets-integration-in-visual-studio-and-visual-studio-code\/","title":{"rendered":"CMake Presets integration in Visual Studio and Visual Studio Code"},"content":{"rendered":"<p><a href=\"https:\/\/cmake.org\/cmake\/help\/latest\/manual\/cmake-presets.7.html\">CMakePresets.json<\/a> was released in CMake 3.19 and 3.20 and allows users to specify common configure, build, and test options and share them with others. We have added support for CMakePresets.json in Visual Studio and the CMake Tools extension for Visual Studio Code. You can now invoke CMake with the same CMakePresets.json file in Visual Studio, in Visual Studio Code, in a Continuous Integration pipeline, and from the CLI on Windows, Linux, and macOS.<\/p>\n<p>Our CMake Presets integration is now available in preview in <a href=\"https:\/\/visualstudio.microsoft.com\/vs\/preview\/\">Visual Studio 2019 version 16.10 Preview 2<\/a> and <a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=ms-vscode.cmake-tools\">CMake Tools version 1.7<\/a>.<\/p>\n<h4>Why should I use CMake Presets?<\/h4>\n<p>CMake released support for <a href=\"https:\/\/cmake.org\/cmake\/help\/latest\/manual\/cmake-presets.7.html#configure-preset\">Configure Presets<\/a> in CMake 3.19. Our team at Microsoft contributed support for <a href=\"https:\/\/cmake.org\/cmake\/help\/latest\/manual\/cmake-presets.7.html#build-preset\">Build Presets<\/a> and <a href=\"https:\/\/cmake.org\/cmake\/help\/latest\/manual\/cmake-presets.7.html#test-preset\">Test Presets<\/a> in CMake 3.20. We\u2019re excited to adopt CMake Presets to address some common problems related to CMake configuration and build.<\/p>\n<ul>\n<li><strong>Move seamlessly between Visual Studio and Visual Studio Code. <\/strong>With the adoption of CMakePresets.json, the same file can be used to drive CMake in both Visual Studio and Visual Studio Code. This makes it easier for developers on your team to hop between Visual Studio and Visual Studio Code and work in the development environment that best suits their needs.<\/li>\n<li><strong>Project-specific information can always be checked in. <\/strong>CMake supports two files: CMakePresets.json and CMakeUserPresets.json. CMakePresets.json is intended to save project-wide presets, while CMakeUserPresets.json is intended for developers to save their own local presets. This ensures project-specific information can always be checked into source control and makes it easier for new team members to get up-and-running. It also makes it easier for teams to synchronize changes to their CMake builds across the entire team. Both CMakePresets.json and CMakeUserPresets.json are supported in Visual Studio and Visual Studio Code.<\/li>\n<li><strong>Reproduce builds in Visual Studio and Visual Studio Code from the CLI. <\/strong>Since CMakePresets.json is supported by CMake itself, the same CMakePresets.json file can be used to invoke CMake in Visual Studio, in Visual Studio Code, in a Continuous Integration pipeline, and from the CLI on Windows, Linux, and macOS. CMake 3.20 or higher is required to build with CMakePresets.json from the CLI.<\/li>\n<li><strong>Streamline build instructions for open source CMake projects. <\/strong>You can pull down any project that has a CMakePresets.json file at the root of the repository and build and test it locally using the instructions encoded in a preset. All command arguments that can be passed to CMake or CTest from the command line can be specified in a Configure, Build, or Test Preset.<\/li>\n<\/ul>\n<p>For more information on CMake Presets, you can check out my recent <a href=\"https:\/\/www.youtube.com\/watch?v=-NhaPNq16Qk\">session at ACCU<\/a> where I demonstrate how to use CMakePresets.json in Visual Studio, from the CLI, and in a GitHub Actions pipeline. You can also view a <a href=\"https:\/\/github.com\/esweet431\/box2d-lite\/blob\/main\/CMakePresets.json\">sample CMakePresets.json file<\/a>, register for <a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/sign-up-for-the-pure-virtual-cpp-2021-conference\/\">Pure Virtual C++<\/a>, or read the <a href=\"https:\/\/cmake.org\/cmake\/help\/latest\/manual\/cmake-presets.7.html\">official CMake documentation<\/a>. More information on CMake Presets integration in Visual Studio and VS Code is below.<\/p>\n<p style=\"text-align: center;\"><iframe src=\"\/\/www.youtube.com\/embed\/-NhaPNq16Qk\" width=\"560\" height=\"314\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<h4>CMake Presets in Visual Studio<\/h4>\n<p>The best resource to learn about our CMake Presets integration in Visual Studio is our <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/build\/cmake-presets-vs?view=msvc-160\">documentation<\/a>. CMake Presets integration in Visual Studio is still in preview.<\/p>\n<p>CMakePresets.json will be a recommended alternative to CMakeSettings.json. Visual Studio will never read from both CMakePresets.json and CMakeSettings.json at the same time. You can enable CMake Presets integration for all projects in <strong>Tools &gt; Options &gt; CMake &gt; General<\/strong>. You must close and reopen the folder in Visual Studio to activate the integration.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2021\/04\/enable.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-27912\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2021\/04\/enable.png\" alt=\"The Tools &gt; Options &gt; CMake dialog in Visual Studio. The option to &quot;Use CMakePresets.json to drive CMake configure, build, and test&quot; is checked and underscored in red.\" width=\"740\" height=\"429\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2021\/04\/enable.png 740w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2021\/04\/enable-300x174.png 300w\" sizes=\"(max-width: 740px) 100vw, 740px\" \/><\/a>You can enable CMake Presets integration for a single project by adding a CMakePresets.json file to the root of the open folder. Again, you must close and reopen the folder to activate the integration. See <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/build\/cmake-presets-vs?view=msvc-160#enable-cmakepresetsjson-integration-in-visual-studio-2019\">Enable CMake Presets integration in Visual Studio 2019<\/a> for more information.<\/p>\n<p>Once CMake Presets integration is enabled, you will see three dropdowns across the menu bar.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2021\/04\/cmakepresets.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-27914\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2021\/04\/cmakepresets.png\" alt=\"The menu bar has 3 dropdowns. The dropdown on the left reads &quot;WSL: ubuntu2004&quot;, the dropdown in the middle reads &quot;ninja-debug&quot;, and the dropdown on the right reads &quot;verbose-build&quot;. \" width=\"664\" height=\"158\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2021\/04\/cmakepresets.png 664w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2021\/04\/cmakepresets-300x71.png 300w\" sizes=\"(max-width: 664px) 100vw, 664px\" \/><\/a><\/p>\n<p>The dropdown on the left indicates the active <strong>Target System<\/strong>. This is the system where CMake will be invoked to configure and build the project. This dropdown lists your local machine, all SSH connections in the <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/linux\/connect-to-your-remote-linux-computer?view=msvc-160#set-up-the-remote-connection\">Connection Manager<\/a> by host name, and all Windows Subsystem for Linux (WSL) installations that Visual Studio can find.<\/p>\n<p>The dropdown in the middle indicates the active <strong>Configure Preset<\/strong>. This is the Configure Preset that will be used when CMake is invoked to generate the project build system. This dropdown lists the union of non-hidden Configure Presets defined in CMakePresets.json and CMakeUserPresets.json that apply to the active <strong>Target System<\/strong>. The active Configure Preset in the image above is ninja-debug.<\/p>\n<p>The dropdown on the right indicates the active <strong>Build Preset<\/strong>. This is the Build Preset that will be used when CMake is invoked to build the project. This dropdown lists the union of non-hidden Build Presets defined in CMakePresets.json and CMakeUserPresets.json that apply to the active <strong>Configure Preset<\/strong>. The active Build Preset in the image above is verbose-build. Check out our documentation on <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/build\/cmake-presets-vs?view=msvc-160#configure-and-build\">CMake configuration and build<\/a> to learn more.<\/p>\n<p>With Visual Studio you can edit, build, and debug your CMake targets on Windows, WSL, and remote systems from the comfort of a single IDE. Visual Studio will automatically copy your source code to the specified target system, but your build tools (CMake, generator, compilers), rsync, zip, and gdb must be already installed. See <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/linux\/download-install-and-setup-the-linux-development-workload?view=msvc-160#options-for-creating-a-linux-environment\">Creating a Linux environment<\/a> for more information.<\/p>\n<p>For more information on adding presets, editing presets, running tests, and more, see our documentation on <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/build\/cmake-presets-vs?view=msvc-160\">CMake Presets integration in Visual Studio<\/a>.<\/p>\n<h4>CMake Presets in the CMake Tools extension for Visual Studio Code<\/h4>\n<p>The best resource to learn about CMake Presets integration in the CMake Tools extension is our <a href=\"https:\/\/github.com\/microsoft\/vscode-cmake-tools\/blob\/main\/docs\/cmake-presets.md\">documentation<\/a>. CMake Presets integration in the CMake Tools extension is still in preview.<\/p>\n<p>CMakePresets.json will be a recommended alternative to kits and variants files. CMake Tools will never read from both CMakePresets.json and kits and variants files at the same time. You can enable CMake Presets integration by modifying the value of cmake.useCMakePresets in settings.json.<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"163\">\n<p style=\"text-align: center;\"><strong>Setting<\/strong><\/p>\n<\/td>\n<td width=\"221\">\n<p style=\"text-align: center;\"><strong>Description<\/strong><\/p>\n<\/td>\n<td width=\"144\">\n<p style=\"text-align: center;\"><strong>Accepted values<\/strong><\/p>\n<\/td>\n<td width=\"96\">\n<p style=\"text-align: center;\"><strong>Default value<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\" width=\"163\">cmake.useCMakePresets<\/td>\n<td style=\"text-align: center;\" width=\"221\">Use CMakePresets.json to drive CMake configure, build, and test<\/td>\n<td style=\"text-align: center;\" width=\"144\">always, never, auto<\/td>\n<td style=\"text-align: center;\" width=\"96\">auto<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p><strong>auto <\/strong>evaluates to <strong>always<\/strong> if there\u2019s a CMakePresets.json in the cmake.sourceDirectory of the active folder. It evaluates to <strong>never<\/strong> if there isn\u2019t a CMakePresets.json file in the cmake.sourceDirectory of the active folder. Set cmake.useCMakePresest to <strong>always <\/strong>or <strong>never<\/strong> to explicitly enable or disable CMake Presets integration for all CMake projects. See <a href=\"https:\/\/github.com\/microsoft\/vscode-cmake-tools\/blob\/main\/docs\/cmake-presets.md#enable-cmakepresetsjson-in-the-cmake-tools-extension\">Enable CMake Presets in the CMake Tools extension<\/a> for more information.<\/p>\n<p>Once CMake Presets integration is enabled, you can run several new commands.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2021\/04\/command-palette.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-27915\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2021\/04\/command-palette.png\" alt=\"The command palette is open in Visual Studio Code and reads &quot;&gt;CMake: Preset&quot;. Several commands specific to CMake Presets are available including &quot;CMake: Add Configure Preset&quot; and &quot;CMake: Select Build Preset&quot;. \" width=\"619\" height=\"189\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2021\/04\/command-palette.png 619w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2021\/04\/command-palette-300x92.png 300w\" sizes=\"(max-width: 619px) 100vw, 619px\" \/><\/a><\/p>\n<p>Use the commands <strong>CMake: Select Configure Preset<\/strong>, <strong>CMake: Select Build Preset<\/strong>, and <strong>CMake: Select Test Preset<\/strong> to select your active presets. Your active presets are displayed in the status bar.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2021\/04\/status-bar.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-27916\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2021\/04\/status-bar.png\" alt=\"Image status bar\" width=\"863\" height=\"41\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2021\/04\/status-bar.png 863w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2021\/04\/status-bar-300x14.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2021\/04\/status-bar-768x36.png 768w\" sizes=\"(max-width: 863px) 100vw, 863px\" \/><\/a><\/p>\n<p>In the image above, <strong>[active-configure-preset]<\/strong> indicates the active Configure Preset, <strong>[active-build-preset] <\/strong>indicates the active Build Preset, and <strong>[active-test-preset] <\/strong>indicates the active Test Preset.<\/p>\n<p>For more information on adding presets, editing presets, running CMake, and more, see our documentation on <a href=\"https:\/\/github.com\/microsoft\/vscode-cmake-tools\/blob\/main\/docs\/cmake-presets.md\">CMake Presets integration in Visual Studio Code<\/a>.<\/p>\n<h4>What&#8217;s next?<\/h4>\n<p>CMake Presets integration in Visual Studio and Visual Studio Code is still in preview. We\u2019ll continue to add to our integration and address your feedback in future release of Visual Studio and the CMake Tools extension for VS Code.<\/p>\n<p>In Visual Studio, the best way to file a bug or suggest a feature is with the <strong>Send Feedback<\/strong> button in the upper right-hand corner of the IDE. See <a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/ide\/feedback-options?view=vs-2019#:~:text=In%20Visual%20Studio%2C%20choose%20the%20feedback%20icon%20in,information%2C%20see%20Suggest%20a%20feature%20for%20Visual%20Studio.\">Visual Studio feedback options<\/a> for more information.<\/p>\n<p>In VS Code, the best way to file a bug or suggest a feature is by creating (or upvoting) an issue in the <a href=\"https:\/\/github.com\/microsoft\/vscode-cmake-tools\/issues\">extension\u2019s GitHub repository<\/a>.<\/p>\n<p>We\u2019d love to learn how your team is adopting CMakePresets.json. If you have feedback or a success story to share, you can contact us at <a href=\"mailto:cmake@microsoft.com\">cmake@microsoft.com<\/a>. You can also reach us on Twitter (<a href=\"https:\/\/twitter.com\/visualc\">@VisualC<\/a>).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>CMakePresets.json was released in CMake 3.19 and 3.20 and allows users to specify common configure, build, and test options and share them with others. We have added support for CMakePresets.json in Visual Studio and the CMake Tools extension for Visual Studio Code. You can now invoke CMake with the same CMakePresets.json file in Visual Studio, [&hellip;]<\/p>\n","protected":false},"author":2953,"featured_media":27915,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[270,1,266,279,275],"tags":[],"class_list":["post-27911","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-announcement","category-cplusplus","category-cmake","category-linux","category-visual-studio-code"],"acf":[],"blog_post_summary":"<p>CMakePresets.json was released in CMake 3.19 and 3.20 and allows users to specify common configure, build, and test options and share them with others. We have added support for CMakePresets.json in Visual Studio and the CMake Tools extension for Visual Studio Code. You can now invoke CMake with the same CMakePresets.json file in Visual Studio, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/27911","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/2953"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=27911"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/27911\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/27915"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=27911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=27911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=27911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}