Building And Packaging C++ Modules In Vs2015
This post makes a brief introduction to the C++ modules (we wished C++17, but we will have to wait). Modules have already been experimentally available in an early implementation in CLang, and now Microsoft is also providing them in Visual Studio 2015. We will see their syntax and how to build them, as introduced in the Visual Studio Blog, and at the same time, we will show how to create and consumes packages with C++ modules with conan C/C++ package manager.
A math C++ module library and package
First we will create a very simple math library with addition and multiplication functions,
which will be implemented in a module, called
MyMath in a filename mymath.ixx. This extension
will be the one used to indicate C++ modules syntax:
Currently, building it requires command line invocation of the MSVC compiler cl.exe. This should be done in the Visual Studio Prompt, or with the Visual Studio environment variables loaded. I usually work in the cmder console in windows, so I prefer to load the environment variables:
When building this file, the compiler will generate the typical object code, and also an IFC file, called “MyMath.ifc”, which is the module interface description metadata. If we want to build an actual static library, we can also do it in the command line:
From the user point of view, the .ifc files can be handled and linked as if they were another libraries, so all we have to do to create a conan package recipe for this code, just besides the *mymath.ixx file:
We can now export the package to the conan local storage, so we can consume it from there:
Consuming the C++ module library
Now, in a different folder, we can create the consuming project, that will link against the library:
Building an executable from this code, the following commands would be required:
We need the library path, if we built the library manually, it would be the path when we generated it. For conan packages, using a recipe to declare the dependency to the library package, and automating the build is very convenient, even if we are not creating a package for the consumer project. Remember, conan recipes are basically convenient python scripts:
With this recipe, installing the required dependencies (in our case, building the library with the MyMath C++ module) is simple:
And then, building and executing the application:
If you want to test this quickly you might just:
We have always considered Windows as a first class citizen (as well as Linux and OSX) in conan, and we design, implement, test and deploy with Windows users in mind (we also develop in Windows about 50% of our time).
We hope that this post proves it, as despite the early stage of the Visual Studio C++ modules build infrastructure (just the command line), conan package manager is able to both create and consume packages quite easily and in an intuitive way. This can be further simplified by improving the existing “Visual Studio” or “CMake” generators, once C++ modules have full support in the IDE and build system.
We are also very excited to be able to test this amazing feature in Visual Studio, undoubtly Microsoft is doing an amazing work on C++ lately, and really looking forward to seeing C++ modules becoming mainstream.