{"id":17385,"date":"2017-11-02T10:36:29","date_gmt":"2017-11-02T17:36:29","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/?p=17385"},"modified":"2021-01-05T10:06:14","modified_gmt":"2021-01-05T10:06:14","slug":"customizing-your-environment-with-visual-c-and-open-folder","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/customizing-your-environment-with-visual-c-and-open-folder\/","title":{"rendered":"Customizing your Environment with Visual C++ and Open Folder"},"content":{"rendered":"<p><a href=\"https:\/\/blogs.msdn.microsoft.com\/c\/2017\/12\/05\/\u4f7f\u7528visual-c\u548copen-folder\u81ea\u5b9a\u4e49\u60a8\u7684\u73af\u5883\/\">\u70b9\u8fd9\u91cc\u770b\u4e2d\u6587\u7248<\/a><\/p>\n<p>Ever since we shipped support for opening a folder of C++ code, the community has been asking for more control over their build and editing environments. \u00a0To achieve this, we have added new ways to customize your environment with CppProperties.json in the latest version of <a href=\"https:\/\/www.visualstudio.com\/vs\/preview\/\">Visual Studio 2017<\/a>.<\/p>\n<p>This new customization surface enables you to use a broader variety of tools, write more succinct CppProperties files, and have powerful per-configuration customization similar to MSBuild.\u00a0 The topics below expand on several concepts described in the original <a href=\"https:\/\/aka.ms\/openfolder\/cpp\">C++ Open Folder<\/a> post.\u00a0 If you are not familiar with editing CppProperties.json, Launch.vs.json, and Tasks.vs.json it might be worth reading that post first.<\/p>\n<p>This post is a companion to our previous post on <a href=\"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2017\/08\/14\/cmake-support-in-visual-studio-customizing-your-environment\/\">customizing your environment for CMake projects<\/a> so if you have already read it, you may find that some of this content will be similar since we strive to keep the experiences consistent.\u00a0 The most important differences are under \u201cWhat about Launch.vs.json and Tasks.vs.json\u201d regarding how to use configuration-specific variables.<\/p>\n<h3>What\u2019s new in CppProperties.json<\/h3>\n<p>The heart of this new flexibility is inside your project\u2019s CppProperties.json file and it stems from two new concepts:<\/p>\n<ol>\n<li>The ability to inherit a set of default environment variables globally or per configuration using the \u201cinheritEnvironments\u201d property.<\/li>\n<li>The ability to define custom environment variables and their values globally or per configuration by defining an \u201cenvironments\u201d block.<\/li>\n<\/ol>\n<p>Combining these new concepts with the existing ability to consume environment variables in CppProperties.json, launch.vs.json, and tasks.vs.json using the \u201c${env.VAR}\u201d syntax, provides a powerful mechanism for creating rich development environments.<\/p>\n<p>Let\u2019s start with a quick example of how using this feature might look:<\/p>\n<pre class=\"prettyprint js\">{\r\n  \/\/ The \"environments\" property is an array of key value pairs of the form\r\n  \/\/ { \"EnvVar1\": \"Value1\", \"EnvVar2\": \"Value2\" }\r\n  \"environments\": [\r\n    {\r\n      \"INCLUDE\": \"${workspaceRoot}\\\\src\\\\includes\"\r\n    }\r\n  ],\r\n\r\n  \"configurations\": [\r\n    {\r\n      \"inheritEnvironments\": [\r\n        \/\/ Inherit the MSVC 32-bit environment and toolchain.\r\n        \"msvc_x86\"\r\n      ],\r\n      \"name\": \"x86\",\r\n      \"includePath\": [\r\n        \/\/ Use the include path defined above.\r\n        \"${env.INCLUDE}\"\r\n      ],\r\n      \"defines\": [ \"WIN32\", \"_DEBUG\", \"UNICODE\", \"_UNICODE\" ],\r\n      \"intelliSenseMode\": \"msvc-x86\"\r\n    },\r\n    {\r\n      \"inheritEnvironments\": [\r\n        \/\/ Inherit the MSVC 64-bit environment and toolchain.\r\n        \"msvc_x64\"\r\n      ],\r\n      \"name\": \"x64\",\r\n      \"includePath\": [\r\n        \/\/ Use the include path defined above.\r\n        \"${env.INCLUDE}\"\r\n      ],\r\n      \"defines\": [ \"WIN32\", \"_DEBUG\", \"UNICODE\", \"_UNICODE\" ],\r\n      \"intelliSenseMode\": \"msvc-x64\"\r\n    }\r\n  ]\r\n}\r\n<\/pre>\n<p>To unpack this a bit, this example defines two configurations that build with Microsoft\u2019s Visual C++ toolchain. The first builds for x86 (since it inherits the \u201cmsvc_x86\u201d environment) while the other builds for x64. It also defines an environment variable \u201cINCLUDE\u201d (line 6) that is used by both configurations.<\/p>\n<p>Keep in mind, both the \u201cenvironments\u201d (line 4) and \u201cinheritEnvironments\u201d (lines 12 and 25) properties can be defined globally for all configurations, per configuration, or both. In the example above, the \u201cINCLUDE\u201d variable will be global and the \u201cinheritEnvironment\u201d property will only apply to each individual configuration.<\/p>\n<p>The following environments are available today:<\/p>\n<ul>\n<li>Target x86 Windows with MSVC (msvc_x86)<\/li>\n<li>Target x64 Windows with MSVC (msvc_x64)<\/li>\n<li>Target x86 Windows with the 64-bit MSVC (msvc_x64_x86)<\/li>\n<li>Target x64 Windows with the 64-bit MSVC (msvc_x64_x64)<\/li>\n<\/ul>\n<p>Additionally, if you have the <a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/linux-development-with-c-in-visual-studio\/\">Linux Workload<\/a> installed, the following environments are available for <a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/remote-tasks-in-visual-studio\/\">remotely targeting<\/a> Linux and WSL:<\/p>\n<ul>\n<li>Target x86 Linux remotely (linux_x86)<\/li>\n<li>Target x64 Linux remotely (linux_x64)<\/li>\n<li>Target ARM Linux remotely (linux_arm)<\/li>\n<\/ul>\n<p>Configuration-specific environment variables are evaluated last, so they override global ones.\u00a0 The example below explains the override behavior in the comments:<\/p>\n<pre class=\"prettyprint js\">{\r\n  \/\/ The \"environments\" property is an array of key value pairs of the form\r\n  \/\/ { \"EnvVar1\": \"Value1\", \"EnvVar2\": \"Value2\" }\r\n  \"environments\": [\r\n    {\r\n      \"INCLUDE\": \"${workspaceRoot}\\\\src\\\\includes\"\r\n    }\r\n  ],\r\n\r\n  \"configurations\": [\r\n    {\r\n      \"inheritEnvironments\": [\r\n        \/\/ Inherit the MSVC 32-bit environment and toolchain.\r\n        \"msvc_x86\"\r\n      ],\r\n      \"name\": \"x86\",\r\n      \"includePath\": [\r\n        \/\/ Use the include path defined above.\r\n        \"${env.INCLUDE}\"\r\n      ],\r\n      \"defines\": [ \"WIN32\", \"_DEBUG\", \"UNICODE\", \"_UNICODE\" ],\r\n      \"intelliSenseMode\": \"msvc-x86\"\r\n    },\r\n    {\r\n      \/\/ The \"environments\" property is an array of key value pairs of the form\r\n      \/\/ { \"EnvVar1\": \"Value1\", \"EnvVar2\": \"Value2\" }\r\n      \"environments\": [\r\n        {\r\n          \/\/ Append 64-bit specific include path to env.INCLUDE.\r\n          \"INCLUDE\": \"${env.INCLUDE};${workspaceRoot}\\\\src\\\\includes64\"\r\n        }\r\n      ],\r\n\r\n      \"inheritEnvironments\": [\r\n        \/\/ Inherit the MSVC 64-bit environment and toolchain.\r\n        \"msvc_x64\"\r\n      ],\r\n      \"name\": \"x64\",\r\n      \"includePath\": [\r\n        \/\/ Use the include path defined above.\r\n        \"${env.INCLUDE}\"\r\n      ],\r\n      \"defines\": [ \"WIN32\", \"_DEBUG\", \"UNICODE\", \"_UNICODE\" ],\r\n      \"intelliSenseMode\": \"msvc-x64\"\r\n    }\r\n  ]\r\n}\r\n<\/pre>\n<p>If you need to declare a lot of variables for your build environment and then make only minor modifications to them for each configuration, this override behavior can condense your project\u2019s CppProperties.json file considerably.<\/p>\n<h3>What about Launch.vs.json and Tasks.vs.json<\/h3>\n<p>In case you are wondering if you can use these variables outside of the CppProperties.json file, the answer is yes!\u00a0 All the environment variables you declare in your CppProperties.json can be consumed in <a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/bring-your-c-codebase-to-visual-studio-with-open-folder\/#debug-code\">launch.vs.json<\/a> and <a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/bring-your-c-codebase-to-visual-studio-with-open-folder\/#build-code\">tasks.vs.json<\/a> as well. \u00a0Just embed the same &#8220;${env.VarName}&#8221; syntax into any property\u2019s value in a task or launch configuration.\u00a0 The macro syntax will be expanded into its actual value, as it is on line 16.<\/p>\n<pre class=\"prettyprint js\">{\r\n  \"version\": \"0.2.1\",\r\n  \"tasks\": [\r\n    {\r\n      \"taskName\": \"build-helloworld\",\r\n      \"appliesTo\": \"*.cpp\",\r\n      \"contextType\": \"build\",\r\n      \"type\": \"launch\",\r\n      \"command\": \"${env.comspec}\",\r\n      \"workingDirectory\": \"${workspaceRoot}\",\r\n      \/\/ Use environment from selected configuration, you can omit this\r\n      \/\/ to only use globally defined variables instead.\r\n      \"inheritEnvironments\": [ \"${cpp.activeConfiguration}\" ],\r\n      \"output\": \"${workspaceRoot}\\\\bin\\\\helloworld.exe\",\r\n      \"args\": [\r\n        \"build.bat ${env.BUILD_ARGS}\"\r\n      ]\r\n    }\r\n  ]\r\n}\r\n<\/pre>\n<p>If the value of an environment variable is configuration-specific, the value for the currently selected configuration when you try to run a task or debug your program will be used if you include this in your task or launch configuration:<\/p>\n<pre class=\"prettyprint js\">\"inheritEnvironments\":  [ \"${cpp.activeConfiguration}\" ]\r\n<\/pre>\n<p>If you do not include this, only globally defined variables will be available.<\/p>\n<p>The environment variables you declare will also be inherited by the processes launched by tasks. Programs being debugged, on the other hand, will not inherit the build environment automatically. The example below shows how to explicitly pass environment variables to a launched process.<\/p>\n<pre class=\"prettyprint js\">{\r\n  \"version\": \"0.2.1\",\r\n  \"defaults\": {},\r\n  \"configurations\": [\r\n    {\r\n      \"type\": \"native\",\r\n      \"name\": \"helloworld.exe\",\r\n      \/\/ Use environment from selected configuration, you can omit this\r\n      \/\/ to only use globally defined variables instead.\r\n      \"inheritEnvironments\":  [ \"${cpp.activeConfiguration}\" ],\r\n      \"project\": \"bin\\\\helloworld.exe\",\r\n      \"args\": [\r\n        \/\/ Use arguments defined in CppProperties.json.\r\n        \"${env.PROG_ARGS}\"\r\n      ] ,\r\n      \"env\": \"var1=${env.var1}\\u0000var2=hardcodedvalue\"\r\n    }\r\n  ]\r\n}\r\n<\/pre>\n<p>You can see on line 14 that it is possible to reference variables defined in your CppProperties.json file. The \u201c\\u0000\u201d on line 17 is a null character used to separate variables.<\/p>\n<h3>Advanced Features<\/h3>\n<p>Those of you with a keen eye might have noticed that \u201cenvironments\u201d and \u201cinheritEnvironments\u201d are arrays in the CppProperties.json syntax.\u00a0 It is possible to declare and inherit from multiple environments.\u00a0 For typical build scenarios it is unlikely that you would want to inherit from more than one environment but there are some cases where you might want to declare more than one environment block.\u00a0 The prime use case for this would be to declare a few variables that you can reference in any CppProperties, Launch, or Tasks JSON but don\u2019t want added to the build environment itself &#8211; e.g. not inherited by spawned build processes.<\/p>\n<p>The following example shows how you accomplish create a custom namespace:<\/p>\n<pre class=\"prettyprint js\">{\r\n  \/\/ The \"environments\" property is an array of key value pairs of the form\r\n  \/\/ { \"EnvVar1\": \"Value1\", \"EnvVar2\": \"Value2\" }\r\n  \"environments\": [\r\n    {\r\n      \"INCLUDE\": \"${workspaceRoot}\\\\src\\\\includes\"\r\n    },\r\n    {\r\n      \/\/ \"namespace\" is a reserved key that lets you put variables\r\n      \/\/ in namespaces other than $env.\r\n      \"namespace\": \"special\",\r\n      \/\/ SpecialVar will not be added to the environment.\r\n      \"SpecialVar\": \"special\"\r\n    }\r\n\r\n  ],\r\n\r\n  \"configurations\": [\r\n    {\r\n      \"inheritEnvironments\": [\r\n        \/\/ Inherit the MSVC 32-bit environment and toolchain.\r\n        \"msvc_x86\"\r\n      ],\r\n      \"name\": \"x86\",\r\n      \"includePath\": [\r\n        \/\/ Use the include path defined above.\r\n        \"${env.INCLUDE}\"\r\n      ],\r\n      \"defines\": [\r\n        \/\/ You can use alternative namespaces (such as special defined above)\r\n        \/\/ just like \"${env.VAR}\"\r\n        \"${special.specialVar}\",\r\n        \"WIN32\", \"_DEBUG\", \"UNICODE\", \"_UNICODE\"\r\n      ],\r\n      \"intelliSenseMode\": \"msvc-x86\"\r\n    }\r\n  ]\r\n}\r\n<\/pre>\n<p>You can access \u201cSpecialVar\u201d in any CppProperties, Launch, or Tasks JSON file with the syntax \u201c${special.SpecialVar}\u201d, as seen on line 32.<\/p>\n<h3>Send Us Feedback<\/h3>\n<p>To try out the latest and greatest C++ features and give us some early feedback, please download and install the latest\u00a0<a href=\"https:\/\/www.visualstudio.com\/vs\/preview\/\">Visual Studio 2017 Preview<\/a>.\u00a0 As always, we welcome your feedback.\u00a0 Feel free to send any comments through e-mail at <a href=\"mailto:visualcpp@microsoft.com\">visualcpp@microsoft.com<\/a>, through\u00a0<a href=\"https:\/\/twitter.com\/visualc\">Twitter @visualc<\/a>, or Facebook at\u00a0<a href=\"https:\/\/www.facebook.com\/Microsoft-Visual-Cpp-222043184527264\/\">Microsoft Visual Cpp<\/a>.<\/p>\n<p>If you encounter other problems with Visual Studio 2017 please let us know via\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/ide\/how-to-report-a-problem-with-visual-studio-2017\">Report a Problem<\/a>, which is available in both the installer and the IDE itself. \u00a0For suggestions, let us know through\u00a0<a href=\"https:\/\/visualstudio.uservoice.com\/forums\/121579-visual-studio-2015\/category\/30937-languages-c\">UserVoice<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u70b9\u8fd9\u91cc\u770b\u4e2d\u6587\u7248 Ever since we shipped support for opening a folder of C++ code, the community has been asking for more control over their build and editing environments. \u00a0To achieve this, we have added new ways to customize your environment with CppProperties.json in the latest version of Visual Studio 2017. This new customization surface enables you [&hellip;]<\/p>\n","protected":false},"author":326,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1,269],"tags":[268],"class_list":["post-17385","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus","category-openfolder","tag-openfolder"],"acf":[],"blog_post_summary":"<p>\u70b9\u8fd9\u91cc\u770b\u4e2d\u6587\u7248 Ever since we shipped support for opening a folder of C++ code, the community has been asking for more control over their build and editing environments. \u00a0To achieve this, we have added new ways to customize your environment with CppProperties.json in the latest version of Visual Studio 2017. This new customization surface enables you [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/17385","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\/326"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=17385"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/17385\/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=17385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=17385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=17385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}