{"id":11835,"date":"2016-11-16T13:00:49","date_gmt":"2016-11-16T20:00:49","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/?p=11835"},"modified":"2019-02-18T18:04:33","modified_gmt":"2019-02-18T18:04:33","slug":"cmake-support-in-visual-studio-the-visual-studio-2017-rc-update","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/cmake-support-in-visual-studio-the-visual-studio-2017-rc-update\/","title":{"rendered":"CMake support in Visual Studio &#8211; the Visual Studio 2017 RC update"},"content":{"rendered":"<p><a href=\"https:\/\/www.visualstudio.com\/visual-studio-pre-release-downloads\/\">Visual Studio 2017 RC<\/a> is an important release when it comes to its support for CMake. The &#8220;Tools for CMake&#8221; VS component is now ready for public preview and we\u2019d like to invite all of you to bring your CMake projects into VS and give us feedback on your experience.<\/p>\n<p>For an overview of the general Visual Studio CMake experience, head over to the <a href=\"https:\/\/aka.ms\/cmake\">announcement post for CMake support in Visual Studio<\/a> that has been updated to include all the capabilities discussed in this post. Additionally, if you\u2019re interested in the \u201cOpen Folder\u201d capability for C++ projects that are not using CMake or MSBuild, check out <a href=\"https:\/\/aka.ms\/openfolder\/cpp\">the Open Folder for C++ announcement blog<\/a>.<\/p>\n<p>The RC release brings support for:<\/p>\n<ul>\n<li><a href=\"#edit-cmake\">Editing CMake projects<\/a><\/li>\n<li><a href=\"#configure-cmake\">Configuring CMake projects<\/a><\/li>\n<li><a href=\"#build-cmake\">Building and debugging of CMake targets<\/a><\/li>\n<\/ul>\n<h3><a>Editing CMake projects<\/a><\/h3>\n<p><strong>Default CMake configurations.<\/strong> As soon as you open a folder containing a CMake project, Solution Explorer will display the files in that folder and you can open any one of them in the editor. In the background, VS will start indexing the C++ sources in your folder. It will also run CMake.exe to collect more information about your CMake project (CMake cache will be generated in the process). CMake is invoked with a specific set of switches that are defined as part of a default CMake configuration that VS creates under the name \u201cVisual Studio 15 x86\u201d.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/cmake-editor-goldbar.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/cmake-editor-goldbar.png\" alt=\"cmake-editor-goldbar\" width=\"515\" height=\"158\" class=\"alignnone size-full wp-image-11965\" \/><\/a><\/p>\n<p><strong>CMake configuration switch.<\/strong> You can switch between CMake configurations from the C++ Configuration dropdown in the General tab. If a configuration does not have the needed information for CMake to correctly create its cache, you can further customize it \u2013 <a href=\"#configure-cmake\">how to configure CMake<\/a> is explained later in the post.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/cmake-configuration-dropdown.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/cmake-configuration-dropdown.png\" alt=\"cmake-configuration-dropdown\" width=\"689\" height=\"122\" class=\"alignnone size-full wp-image-11945\" \/><\/a><\/p>\n<p><strong>Auto-update CMake cache.<\/strong> If you make changes to the CMakeLists.txt files or change the active configuration, the CMake generation step will automatically rerun. You can track its progress in the CMake output pane of the <strong>Output Window<\/strong>.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/cmake-editor-goldbar-2.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/cmake-editor-goldbar-2.png\" alt=\"cmake-editor-goldbar-2\" width=\"457\" height=\"135\" class=\"alignnone size-full wp-image-11975\" \/><\/a><\/p>\n<p>When the generation step completes, the notification bar in editors is dismissed, the Startup Item dropdown will contain the updated list of CMake targets and C++ IntelliSense will incrementally update with the latest changes you made (e.g. adding new files, changing compiler switches, etc.)<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/cmake-debug-target.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/cmake-debug-target.png\" alt=\"cmake-debug-target\" width=\"708\" height=\"227\" class=\"alignnone size-full wp-image-11955\" \/><\/a><\/p>\n<h3><a>Configure CMake projects<\/a><\/h3>\n<p><strong>Configure CMake via CMakeSettings.json.<\/strong> If your CMake project requires additional settings to configure the CMake cache correctly, you can customize these settings by creating a <em>CMakeSettings.json<\/em> file in the same folder with the root CMakeLists.txt. In this file you can specify as many CMake configurations as you need \u2013 you will be able to switch between them at any time.<\/p>\n<p>You can create the CMakeSettings.json file by selecting the <strong>Project<\/strong> &gt; <strong>Edit Settings<\/strong> &gt; <em>path-to-CMakeLists (configuration-name)<\/em> menu entry.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/cmake-editsettings.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/cmake-editsettings.png\" alt=\"cmake-editsettings\" width=\"621\" height=\"105\" class=\"alignnone size-full wp-image-11985\" \/><\/a><\/p>\n<p><strong>CMakeSettings.json example<\/strong><\/p>\n<pre class=\"prettyprint\">{\n  &quot;configurations&quot;: [\n   {\n    &quot;name&quot;: &quot;my-config&quot;,\n    &quot;generator&quot;: &quot;Visual Studio 15 2017&quot;,\n    &quot;buildRoot&quot;: &quot;${env.LOCALAPPDATA}\\\\CMakeBuild\\\\${workspaceHash}\\\\build\\\\${name}&quot;,\n    &quot;cmakeCommandArgs&quot;: &quot;&quot;,\n    &quot;variables&quot;: [\n     {\n      &quot;name&quot;: &quot;VARIABLE&quot;,\n      &quot;value&quot;: &quot;value&quot;\n     }\n    ]\n  }\n ]\n}<\/pre>\n<p>If you already have CMake.exe working on the command line, creating a new CMake configuration in the CMakeSettings.json should be trivial:<\/p>\n<ul>\n<li><strong>name<\/strong>: is the configuration name that will show up in the C++ configuration dropdown. This property value can also be used as a macro ${name} to specify other property values e.g. see \u201cbuildRoot\u201d definition<\/li>\n<li><strong>generator<\/strong>: maps to <strong>-G switch<\/strong> and specifies the generator to be used. This property can also be used as a macro ${generator} to help specify other property values. VS currently supports the following CMake generators:\n<ul>\n<li>\u201cVisual Studio 14 2015\u201d<\/li>\n<li>\u201cVisual Studio 14 2015 ARM\u201d<\/li>\n<li>\u201cVisual Studio 14 2015 Win64\u201d<\/li>\n<li>\u201cVisual Studio 15 2017\u201d<\/li>\n<li>\u201cVisual Studio 15 2017 ARM\u201d<\/li>\n<li>\u201cVisual Studio 15 2017 Win64\u201d<\/li>\n<\/ul>\n<\/li>\n<li><strong>buildRoot<\/strong>: maps to <strong>-DCMAKE_BINARY_DIR switch<\/strong> and specifies where the CMake cache will be created. If the folder does not exist, it will be created<\/li>\n<li><strong>variables<\/strong>: contains a name+value pair of CMake variables that will get passed as <strong>-Dname=value<\/strong> to CMake. If your CMake project build instructions specify adding any variables directly to the CMake cache file, it is recommended that you add them here instead.<\/li>\n<li><strong>cmakeCommandArgs<\/strong>: specifies any additional switches you want to pass to CMake.exe<\/li>\n<\/ul>\n<p><strong>CMakeSettings.json file IntelliSense.<\/strong> When you have the JSON editor installed (it comes with the Web Development Workload), JSON intelliSense will assist you while\u00a0making changes to the CMakeSettings.json file.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/cmake-settings-intellisense.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/cmake-settings-intellisense.png\" alt=\"cmake-settings-intellisense\" width=\"680\" height=\"237\" class=\"alignnone size-full wp-image-12025\" \/><\/a><\/p>\n<p><strong>Environment variable support and macros.<\/strong> CMakeSettings.json supports consuming environment variables for any of the configuration properties. The syntax to use is ${env.FOO} to expand the environment variable %FOO%.<\/p>\n<p>You also have access to built-in macros inside this file:<\/p>\n<ul>\n<li><em>${workspaceRoot}<\/em> \u2013 provides the full path to the workspace folder<\/li>\n<li><em>${workspaceHash}<\/em> \u2013 hash of workspace location; useful for creating a unique identifier for the current workspace (e.g. to use in folder paths)<\/li>\n<li><em>${projectFile}<\/em> \u2013 the full path for the root CMakeLists.txt<\/li>\n<li><em>${projectDir}<\/em> \u2013 the full path to the folder of the root CMakeLists.txt file<\/li>\n<li><em>${thisFile}<\/em> \u2013 the full path to the CMakeSettings.json file<\/li>\n<li><em>${name}<\/em> \u2013 the name of the configuration<\/li>\n<li><em>${generator}<\/em> \u2013 the name of the CMake generator used in this configuration<\/li>\n<\/ul>\n<h3><a>Building and debugging CMake projects<\/a><\/h3>\n<p><strong>Customize build command.<\/strong> By default, VS invokes MSBuild with the following switches: -m -v:minimal. You can customize this command, by changing the \u201cbuildCommandArgs\u201d configuration property in CMakeSettings.json<\/p>\n<p><strong>CMakeSettings.json<\/strong><\/p>\n<pre class=\"prettyprint\">{\n  &quot;configurations&quot;: [\n   {\n     &quot;name&quot;: &quot;x86&quot;,\n     &quot;generator&quot;: &quot;Visual Studio 15 2017&quot;,\n     &quot;buildRoot&quot;: &quot;${env.LOCALAPPDATA}\\\\CMakeBuild\\\\${workspaceHash}\\\\build\\\\${name}&quot;,\n     &quot;cmakeCommandArgs&quot;: &quot;&quot;,\n     &quot;buildCommandArgs&quot;: &quot;-m:8 -v:minimal -p:PreferredToolArchitecture=x64&quot;\n   }\n ]\n}<\/pre>\n<h3>Call to action<\/h3>\n<p><a href=\"https:\/\/www.visualstudio.com\/visual-studio-pre-release-downloads\/\">Download Visual Studio 2017 RC<\/a> today and try the \u201cOpen Folder\u201d experience for CMake projects. For an overview of the CMake experience, also check out the <a href=\"https:\/\/aka.ms\/cmake\">CMake support in Visual Studio blog post<\/a>.\nIf you\u2019re using CMake when developing your C++ projects, we would love to hear from you! Please share your feedback in the comments below or through the \u201cSend Feedback\u201d icon in VS.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Visual Studio 2017 RC is an important release when it comes to its support for CMake. The &#8220;Tools for CMake&#8221; VS component is now ready for public preview and we\u2019d like to invite all of you to bring your CMake projects into VS and give us feedback on your experience. For an overview of the [&hellip;]<\/p>\n","protected":false},"author":308,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[270,266],"tags":[8,140,252,233,308,6,309,268,185],"class_list":["post-11835","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-announcement","category-cmake","tag-announcement","tag-c","tag-cmake","tag-cross-platform","tag-existing-code","tag-ide","tag-import","tag-openfolder","tag-visual-studio"],"acf":[],"blog_post_summary":"<p>Visual Studio 2017 RC is an important release when it comes to its support for CMake. The &#8220;Tools for CMake&#8221; VS component is now ready for public preview and we\u2019d like to invite all of you to bring your CMake projects into VS and give us feedback on your experience. For an overview of the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/11835","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\/308"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=11835"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/11835\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=11835"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=11835"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=11835"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}