📜 ⬆️ ⬇️

Using C ++ Modules in Visual Studio 2017

What's new?


The Visual C ++ team is pleased to announce that in Visual Studio 2017, the quality of the implementation of C ++ modules has been significantly improved according to the technical specifications ; We also added the ability to connect the Standard C ++ Library through module interfaces . These interfaces, like the support of the modules by the compiler, are experimental designs and will be developed in accordance with the standardization process.

Beginning of work


Standard Library module support is implemented in Visual Studio since version 2017 RTM. This feature is currently optional and disabled by default. In future versions, the modules will be installed automatically along with the headers of the Standard Library. You only need to select this option when installing or upgrading C ++ support.

Picture 5



If you have already installed VS 2017, but have not installed the modules, this is easy to fix. Just run the installer again and select the appropriate components.
')

Picture 23


Verification of installation


To check if your copy of VS 2017 is configured to support modules, compile and run the program below (name it, for example, test-vs2017-slm.cxx ) from the developer’s command line. Since the modules are currently an experimental function, their support is still very poorly implemented in the VS environment.

import std.core; int main() { using namespace std; vector<string> v { "Plato", "Descartes", "Bacon" }; copy(v.begin(), v.end(), ostream_iterator<string>(cout, "\n")); } 

When compiling this code with the command

 cl /experimental:module /EHsc /MD /std:c++latest test-vs2017-slm.cxx 

the output should be an executable file ( test-vs2017-slm.exe ), which at startup prints the words "Plato", "Descartes" and "Bacon" - each in a separate line.

Compiler key for connecting Standard Library modules


You need to add the / MD switch when compiling the source file to connect the Standard Library modules. The / MD key initializes the C runtime dynamic library (CRT) . In debug mode, use the / MDd switch .

If you forgot to specify the / MD key (or / MDd in debug mode), the linker will issue a series of warnings and an LNK2019 link error , indicating that there are unresolved external characters.

No other keys are required to use the Standard Library modules. These modules can only be used for use with the Universal CRT Library (UCRT) DLL import libraries.

Connecting Standard Library modules from VS development environment


If you want to use the development environment instead of the command line, configure your project to use the experimental modules according to the following instructions.

1. Open the project Properties window:

Picture 10


2. Go to the “Configuration Properties” -> C / C ++ -> “Code Generation” section and make sure that you have selected the Multithreaded Debug DLL or Multithreaded DLL library (for debug and release modes, respectively) . These libraries are selected by default for new projects, so if you didn’t change anything, no problems should arise.

Picture 22


3. Go to the “Configuration Properties” section -> C / C ++ -> “Language” and make sure that support for the C ++ 17 standard is enabled. If this is not the case, select from the drop-down list the C ++ 17 standard or the latest C ++ standard project (C ++ Latest Draft Standard) for the configurations you plan to use.

Picture 15


4. Enter the command / experimental: module / module: stdIfcDir "$ (VCToolsInstallDir_150) ifc \ $ (PlatformTarget) " in the section "Configuration Properties” -> C / C ++ -> “Command Line” (Command Line), to enable module support for the current project. Please note that this step will be abolished in future versions of VS 2017: the environment itself will indicate the location of the module files (specified by the / module parameter : stdIfcDir ) when the C ++ modules support option is enabled.

Picture 17



After these actions, the build and launch of the test program should be successful - the program will print the names of the three philosophers.

Picture 20


Modifying module export syntax


At the congress of the C ++ Standardization Committee in November 2016, it was decided to change the syntax of the export of modules (see Module Problem N1 ).

It was:

 export module Bank; 

It became:

 export import Bank; 

The current version of Visual C ++ takes into account this change, but also allows using the old syntax, warning about the transition to the obsolete version. The C ++ committee is considering the possibility of assigning the old syntax a new value that is incompatible with the old one. We encourage you to use the new syntax; support for the old syntax will be discontinued in order to comply with the draft technical specification for the modules according to the ISO C ++ committee amendments.

Modules of the Standard Library (experimental function)


A key innovation in the VS2017 RTM version is support for connecting the Standard C ++ Library through modules. This is an experimental tool, described in the C ++ proposal for Standard Library Modules . In the current version, the modules are organized as follows:


To use these modules in your program, simply type in the top level of the source file the import M instruction, where M is the name of the module from the list above. See test example .

If you want to use modules to include non-Standard Library headers, you can generate Standard Library modules using the / module: name keys (see the original note on C ++ modules ) and / module: export . If your project depends on other libraries and you want to try to collect code without any headers at all, you can pack headers from such libraries in the same way.

The new versions of VS will be more consistent with the proposal for modules of the Standard Library.

Call to action


Download Visual Studio 2017 and try out the modules with your C ++ projects and programs. For starters, you can simply replace all #include standard headers for containers and algorithms with import std.core in the source files and add the / experimental: compilation keys : module and / MD or / MDd (in debug mode) to the build definition. Tell us about the results.

Finally


As always, we welcome your feedback. Send your comments to visualcpp@microsoft.com or post on Twitter @visualc or on the Microsoft Visual Cpp Facebook page.

Other issues related to using the MSVC environment in VS 2017 can be reported using the Report a Problem feature from the installer or from Visual Studio itself. Leave your suggestions on the UserVoice website. Thank!

Source: https://habr.com/ru/post/328482/


All Articles