Getting Started with Modules in C++

Visual Studio 2015 Update 1 shipped with experimental support for a module system for C++. You can learn about it from this talk given by Gabriel Dos Reis at CppCon. Creating and consuming modules is very simple, but getting started with the compiler is not that obvious. At least it wasn’t very obvious to me, so here’s a quick introduction to get you started.

Start by creating an interface source file:

C:\modules>type dog.ixx

#include <stdio.h>

module dog;

export namespace dog
{
    void woof()
    {
        printf("woof!\n");
    }
}

You can use an extension other than .ixx but then you need to tell the compiler that this is the interface to your module. Anything included before the module declaration is not part of the module’s interface but rather merely an implementation detail. So this module will export the dog namespace and its woof function. Nothing else. Go ahead and compile it as follows:

C:\modules>cl -experimental:module -c dog.ixx

Notice the module switch enables support for modules, much like the await switch enables support for coroutines. And since we’re not building an executable, the -c switch instructs the compiler not to involve the linker just yet. The compiler dutifully emits the module artifacts:

C:\modules>dir /b dog.*
dog.ifc
dog.ixx
dog.obj

The .ifc file contains the metadata for the module. The compiler also produces an .obj file that is used to link the module to produce an executable. I can now write an app that relies on this module:

C:\modules>type app.cpp

import dog;

int main()
{
    dog::woof();
}

The import directive tells the compiler to look for the dog module and the main function can call the woof method without having to include any additional declarations. That’s pretty awesome! Compiling the app is also quite straitghtforward:

C:\modules>cl -experimental:module app.cpp dog.obj

The compiler, with the help of the linker this time, successfully imports, compiles, and links the application:

C:\modules>dir /b app.*
app.cpp
app.exe
app.obj

The app prints woof:

C:\modules>app.exe
woof!

There are a variety of other compiler switches to control search paths and other defaults, but this should get you started with modules in Visual C++ 2015 Update 1. Enjoy!

Update: The Visual C++ team just posted a more detailed description.

4 thoughts on “Getting Started with Modules in C++

  1. mohamad

    Hi Kenny
    I used your code in Modern C++ Concurrency on Visual Studio 2015 BUT i see ERROR

    auto get_exclusive() throw() -> auto_lock
    {
    enter();
    return auto_lock(*this, &slim_lock::exit);
    }

    ERROR -> function “auto_lock::auto_lock(auto_lock &&) [with T=slim_lock]” (declared at line 16) cannot be referenced — it is a deleted function

    What do I do ??
    Thanks.

    Reply
    1. Kenny Kerr Post author

      Visual Studio 2015 improved its support for move semantics. At a glance, you should be able to just introduce defaulted declarations of the move constructor and move assignment and the compiler will be satisfied and allow you to return an auto_lock by value.

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s