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.
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.
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.
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.
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 [...]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With