Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What can I do with C++ modules in Visual Studio 2015 so far? (using the experimental switch)

So I was looking at a video showing the news in 'Visual Studio 2015 Update 1' and they mentioned experimental C++ module support (about 8 minutes in).

How much of this feature is actually supported in this version?

I would love if someone would show some kind of code example that works with the Visual Studio /experimental switch, so that I can start playing around with it.

like image 362
01F0 Avatar asked Nov 22 '15 19:11

01F0


People also ask

Does Visual Studio support modules?

As of Visual Studio 2022 version 17.1, C++20 standard modules are fully implemented in the Microsoft C++ compiler. You can use the modules feature to create single-partition modules and to import the Standard Library modules provided by Microsoft.

Does CMake support C++ modules?

CMake currently does not support C++20 modules. See also the relevant issue in the CMake issue tracker. Note that supporting modules requires far more support from the build system than inserting a new compiler option.


2 Answers

Here's how to get a simple example working with the Update 1 RTM, based on the instructions in the video linked in @dxiv's answer.

First, the module definition file mine.ixx. Compile it with: cl /c /experimental:module mine.ixx. This will make mine.obj and mine.ifc:

module mine;
export
{
  int sum(int x, int y);
}

int sum(int x, int y) { return x + y; }

Next main.cpp that uses the module, compile with cl /c /experimental:module main.cpp. This will make main.obj:

#include <iostream>
import mine;

int main()
{
  std::cout << sum(2000, 15) << std::endl;
  return 0;
}

Then link these with link *.obj, and you should get a main.exe.

Note that this doesn't work very well from inside VS at the moment since it doesn't understand the ordering requirements that the modules impose - you'd have to manually modify your project files to do this.

like image 118
porges Avatar answered Sep 20 '22 14:09

porges


See the CppCon 2015 presentation of Gabriel Dos Reis “Large Scale C++ with Modules: What You Should Know" at https://www.youtube.com/watch?v=RwdQA0pGWa4.

From http://nibblestew.blogspot.com/2015/10/some-comments-on-c-modules-talk.html:

The way modules are used (at around 40 minutes in the presentation) has a nasty quirk. The basic approach is that you have a source file foo.cpp, which defines a module Foobar. To compile this you should say this:

cl -c /module foo.cxx

The causes the compiler to output foo.o as well as Foobar.ifc, which contains the binary definition of the module. To use this you would compile a second source file like this:

cl -c baz.cpp /module:reference Foobar.ifc

This is basically the same way that Fortran does its modules [...]

like image 22
dxiv Avatar answered Sep 22 '22 14:09

dxiv