Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing external C++20 modules in Visual Studio 2022

I'm exploring C++20's modules in Visual Studio 2022 and I'm looking into external modules; modules that have been compiled in one project, I'll call it the library project, and are imported in another. To my understanding, this can be done by for example having the library project compile as a static library and generate a .lib file, which then can be added as a dependency in the other project. Then, to add module interfaces from the library project, I can add each module interface by adding its corresponding .ixx.ifc file to C/C++ -> Additional Module Dependencies.

This works fine, but I'm wondering if there is a better way of doing it, as specifying each .ixx.ifc file would get messy for large libraries. Perhaps there is a way to combine modules into larger ones that can be imported without the smaller ones having to be imported? I've tried adding a new module and export import-ing other modules in it, but I still seem to have to add each of those other modules' .ixx.ifc files as dependencies. Or perhaps there is a way to add multiple .ixx.ifc files as dependencies at once?

like image 263
BubLblckZ Avatar asked Feb 26 '26 18:02

BubLblckZ


1 Answers

Project References are Generally Sufficient

In Visual Studio, project references to dependency projects are generally sufficient.

"...In most cases, you won't need to specify this switch because the project system discovers module dependencies within a solution automatically..."
https://learn.microsoft.com/en-us/cpp/build/reference/module-reference?view=msvc-170


Details of the Automatic Module Lookup Process

Module references are provided to the compiler automatically by a two-phase process.

Phase I

The project system generates json files for all imported modules of all projects being built.

(Then, simply by reading all of the module json files in the object output directory(ies), all of the module dependency locations for all of the modules that are imported by all of the sources in the projects that are being built, can be determined. See Phase II).

Phase II

At this stage, the build system has the location of the ifc files of every module used across all projects that are being built. With that information, all cl /reference args are automatically added during compilation.

For this reason, adding /reference args is generally not necessary.

https://lists.isocpp.org/sg15/att-1234/C___20_Modules_Build_in_Visual_Studio.pdf


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!