{"id":15596,"date":"2017-05-05T14:00:52","date_gmt":"2017-05-05T22:00:52","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/?p=15596"},"modified":"2019-02-18T17:48:30","modified_gmt":"2019-02-18T17:48:30","slug":"cpp-modules-in-visual-studio-2017","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/cpp-modules-in-visual-studio-2017\/","title":{"rendered":"Using C++ Modules in Visual Studio 2017"},"content":{"rendered":"<p><a href=\"https:\/\/blogs.msdn.microsoft.com\/c\/2017\/06\/01\/%E5%9C%A8visual-studio-2017-%E4%B8%AD%E4%BD%BF%E7%94%A8c-modules\/\">\u70b9\u8fd9\u91cc\u770b\u4e2d\u6587\u7248<\/a><\/p>\n<p><em>This post was written by <a href=\"https:\/\/social.msdn.microsoft.com\/profile\/Gabriel+Dos+Reis\">Gabriel Dos Reis<\/a>, <a href=\"https:\/\/social.msdn.microsoft.com\/profile\/Andrew+Pardoe+%5BMSFT%5D\">Andrew Pardoe<\/a>, and Billy O\u2019Neal\u00a0<\/em><\/p>\n<h4>What Is New?<\/h4>\n<p>The Visual C++ Team is elated to announce that with Visual Studio 2017, it has substantially improved the quality of the <a href=\"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2015\/12\/03\/c-modules-in-vs-2015-update-1\/\">C++ Modules TS implementation in Visual Studio<\/a>, in addition to introducing ability to consume the <a href=\"#sl-modules\">C++ Standard Library via module interfaces<\/a>.\u00a0 These module interfaces for the Standard Library, just like the compiler support for modules, are experimental and will evolve to track the standardization process.<\/p>\n<h4>Getting Started<\/h4>\n<p>Standard Library Modules support is included in Visual Studio 2017 RTM or newer. This capability is currently optional and off by default. In future versions, they will be installed by default alongside the Standard Library headers. Just choose this option when installing or updating your C++ support.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/vsinstall-stdifcs.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/vsinstall-stdifcs-1024x572.png\" alt=\"Install Standard Library Modules\" width=\"879\" height=\"491\" class=\"alignnone size-large wp-image-15597\" \/><\/a><\/p>\n<p>If you&#8217;ve already installed VS 2017 and didn&#8217;t install the Standard Library Modules, it&#8217;s easy to fix. Just rerun the installer to modify your installation options.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/vsinstall-reruninstaller.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/vsinstall-reruninstaller.png\" alt=\"Rerun VS Installer\" width=\"392\" height=\"680\" class=\"alignnone size-full wp-image-15605\" \/><\/a><\/p>\n<h4 id=\"test\">Testing Your Installation<\/h4>\n<p>To verify that you have VS 2017 set up to take advantage of Standard Library Modules, compile and run this simple program (say in a file <code>test-vs2017-slm.cxx<\/code>) from a Developer Command Prompt. As the feature is still experimental, there is very little built-in support in the VS IDE for modules at this point.<\/p>\n<pre class=\"prettyprint\">\nimport std.core;\u00a0\n\u00a0\nint main() {\u00a0\n\u00a0\u00a0\u00a0 using namespace std;\u00a0\n\u00a0\u00a0\u00a0 vector&lt;string&gt; v { &quot;Plato&quot;, &quot;Descartes&quot;, &quot;Bacon&quot; };\u00a0\n\u00a0\u00a0\u00a0 copy(v.begin(), v.end(), ostream_iterator&lt;string&gt;(cout, &quot;\\n&quot;));\u00a0\n}\u00a0\n<\/pre>\n<p>with the command<\/p>\n<pre class=\"prettyprint disable-colors\">\ncl \/experimental:module \/EHsc \/MD \/std:c++latest test-vs2017-slm.cxx\u00a0\n<\/pre>\n<p>That should effortlessly produce an executable (<code>test-vs2017-slm.exe<\/code>) that, when executed, prints Plato, Descartes, and Bacon, each on a new line.<\/p>\n<h4>Compiler Switch for Consuming Standard Library Modules<\/h4>\n<p>You need to add the compiler switch <code>\/MD<\/code> when compiling a source file that consumes the standard library modules.\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/c-runtime-library\/crt-library-features\">The <code>\/MD<\/code> switch brings in the dynamic library for the CRT<\/a>. In a debug build, you need to use <code>\/MDd<\/code> instead of <code>\/MD<\/code>.<\/p>\n<p>If you forget to specify <code>\/MD<\/code> (or <code>\/MDd<\/code> for debug builds), you will get warnings from the linker and eventually a linker error <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/error-messages\/tool-errors\/linker-tools-error-lnk2019\"><code>LNK2019<\/code><\/a> indicating unresolved external symbols.<\/p>\n<p>No other option is needed to take advantage of the Standard Library Modules support. The Standard Library modules are provided only for use with the DLL import libraries of the UCRT.<\/p>\n<h4>Consuming Standard Library Modules from the VS IDE<\/h4>\n<p>If you want to use the IDE instead of the command line, you can configure your project to use experimental modules according to the following steps.<\/p>\n<ol>\n<li>First, open Properties for the project you want to use:<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/modules-properties.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/modules-properties-1024x818.png\" alt=\"Open Project Properties\" width=\"879\" height=\"702\" class=\"alignnone size-large wp-image-15685\" \/><\/a><\/li>\n<li>Next, under Configuration Properties -&gt; C\/C++ -&gt; Code Generation, verify that Multithreaded Debug DLL or Multithreaded DLL (for Debug and Release, respectively) are set. These are the default options for new projects, so if you have not changed these settings everything should work.<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/modules-usedynamiclibs.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/modules-usedynamiclibs.png\" alt=\"Use dynamic libraries\" width=\"838\" height=\"597\" class=\"alignnone size-full wp-image-15635\" \/><\/a><\/li>\n<li>Next, make sure C++17 features are enabled under Configuration Properties -&gt; C\/C++ -&gt; Language, by selecting C++17 or C++ Latest Draft Standard for any configurations you want to use.<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/modules-latestdraftstandard.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/modules-latestdraftstandard.png\" alt=\"Use latest draft standard\" width=\"838\" height=\"597\" class=\"alignnone size-full wp-image-15645\" \/><\/a><\/li>\n<li>Lastly, if you are using a version of Visual Studio 2017 before 15.3, add <code>\/experimental:module \/module:stdIfcDir \"$(VCToolsInstallDir_150)ifc\\$(PlatformTarget)\"<\/code> to Configuration Properties -&gt; C\/C++ -&gt; Command Line to turn on modules for that project. Note that this step is not necessary in later updates of VS 2017: the VS IDE provides the location of the standard library modules files (the <code>\/module:stdIfcDir<\/code> parameter) for you when you choose to enable C++ Modules. Also, if you&#8217;re using a <a href=\"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2016\/04\/26\/stay-up-to-date-with-the-visual-c-tools-on-nuget\">compiler from a nightly NuGet package<\/a> we&#8217;ve made some fixes so you should set Command Line to <code>\/experimental:module \/module:stdIfcDir \"$(VC_IFCPath)\"<\/code>.<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/modules-commandline.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/modules-commandline.png\" alt=\"Modules configuration properties\" width=\"838\" height=\"597\" class=\"alignnone size-full wp-image-15695\" \/><\/a><\/li>\n<\/ol>\n<p>Now build and test run should succeed, showing you the names of three philosophers.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/modules-build-test-run.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/modules-build-test-run-1024x815.png\" alt=\"Running modules test\" width=\"879\" height=\"700\" class=\"alignnone size-large wp-image-15706\" \/><\/a><\/p>\n<h4>Module Exportation Syntax Change<\/h4>\n<p>At the November 2016 C++ standards meeting, the C++ standards committee changed the syntax for exporting a module (see <a href=\"http:\/\/www.open-std.org\/JTC1\/SC22\/WG21\/docs\/papers\/2016\/p0500r0.pdf\">Module Issue #1<\/a>) from<\/p>\n<pre class=\"prettyprint\">export module Bank;<\/pre>\n<p>to<\/p>\n<pre class=\"prettyprint\">export import Bank;<\/pre>\n<p>This release of Visual C++ implements that resolution, in addition to allowing the old syntax with a warning. The C++ committee is considering repurposing the old syntax, with an incompatible meaning.\u00a0 We encourage you to convert to the new syntax; support for the old syntax will be discontinued to implement the Module TS draft as amended by the ISO C++ standards committee.<\/p>\n<h4 id=\"sl-modules\">Standard Library Modules (Experimental)<\/h4>\n<p>A key novelty in VS2017 RTM release is support for consuming the C++ Standard Library via modules.\u00a0 This is an experimental feature and described in the C++ proposal <a href=\"http:\/\/www.open-std.org\/JTC1\/SC22\/WG21\/docs\/papers\/2017\/p0581r0.pdf\">Standard Library Modules<\/a>. In this release, the modules are organized as follows:<\/p>\n<ul>\n<li><code>std.regex<\/code> provides the content of header <code>&lt;regex&gt;<\/code><\/li>\n<li><code>std.filesystem<\/code> provides the content of header <code>&lt;experimental\/filesystem&gt;<\/code><\/li>\n<li><code>std.memory<\/code> provides the content of header <code>&lt;memory&gt;<\/code><\/li>\n<li><code>std.threading<\/code> provodes the contents of headers <code>&lt;atomic&gt;<\/code>, <code>&lt;condition_variable&gt;<\/code>, <code>&lt;future&gt;<\/code>, <code>&lt;mutex&gt;<\/code>, <code>&lt;shared_mutex&gt;<\/code>, <code>&lt;thread&gt;<\/code><\/li>\n<li><code>std.core<\/code> provides everything else in the C++ Standard Library<\/li>\n<\/ul>\n<p>To use any of these modules in your program, just write <code>import <em>M<\/em>;<\/code> at toplevel in your source file, where <em>M<\/em> is any of the modules listed above. See the <a href=\"#test\">test example<\/a>.<\/p>\n<p>If you want to use modules for header other than the standard library headers, the standard library modules are produced using the <code>\/module:export<\/code> switch described in the <a href=\"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2015\/12\/03\/c-modules-in-vs-2015-update-1\/\">initial C++ modules blog post<\/a>; with <code>\/module:export<\/code>. If you have other libraries on which you depend and want to try a completely header-free experience, you can package those other headers in the same way.<\/p>\n<p>Future releases will more closely track the Standard Library Modules proposal.<\/p>\n<h4>Call to Action<\/h4>\n<p>Download Visual Studio 2017 today and try out modules in your C++ projects and programs.\u00a0 You can start with just replacing all <code>#include<\/code>s of standard headers for containers and algorithms with a simple <code>import std.core;<\/code> in your source files and adding the compiler switches <code>\/experimental:module<\/code> and <code>\/MD<\/code> or <code>\/MDd<\/code> (if you&#8217;re building as debug) to your build definition.\u00a0 Let us know about your experience.<\/p>\n<h4>In Closing<\/h4>\n<p>As always, we welcome your feedback. Feel free to send any comments through e-mail at <a>visualcpp@microsoft.com<\/a>, through <a href=\"https:\/\/twitter.com\/visualc\">Twitter @visualc<\/a>, or Facebook at <a href=\"https:\/\/www.facebook.com\/Microsoft-Visual-Cpp-222043184527264\/\">Microsoft Visual Cpp<\/a>.<\/p>\n<p>If you encounter other problems with MSVC in VS 2017 please let us know via the <a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/ide\/how-to-report-a-problem-with-visual-studio-2017\">Report a Problem<\/a> option, either from the installer or the Visual Studio IDE itself. For suggestions, let us know through <a href=\"https:\/\/visualstudio.uservoice.com\/forums\/121579-visual-studio-2015\/category\/30937-languages-c\">UserVoice<\/a>. Thank you!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u70b9\u8fd9\u91cc\u770b\u4e2d\u6587\u7248 This post was written by Gabriel Dos Reis, Andrew Pardoe, and Billy O\u2019Neal\u00a0 What Is New? The Visual C++ Team is elated to announce that with Visual Studio 2017, it has substantially improved the quality of the C++ Modules TS implementation in Visual Studio, in addition to introducing ability to consume the C++ Standard [&hellip;]<\/p>\n","protected":false},"author":312,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[246],"class_list":["post-15596","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus","tag-modules"],"acf":[],"blog_post_summary":"<p>\u70b9\u8fd9\u91cc\u770b\u4e2d\u6587\u7248 This post was written by Gabriel Dos Reis, Andrew Pardoe, and Billy O\u2019Neal\u00a0 What Is New? The Visual C++ Team is elated to announce that with Visual Studio 2017, it has substantially improved the quality of the C++ Modules TS implementation in Visual Studio, in addition to introducing ability to consume the C++ Standard [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/15596","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\/312"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=15596"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/15596\/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=15596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=15596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=15596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}