C++20 with Visual Studio 2022

Notes on configuring C++20 Modules

2023/02

In the Visual Studio installer, install the individual component

C++ Modules for v143 build tools (x64/x86 - experimental)

Create an Empty Project (C++)

  • Project name: hello_world
  • Project Properties, All Configurations:
  • C/C++ -> All Options
  • C++ Language Standard -> ISO C++20 Standard
  • Scan Sources for Module Dependencies -> Yes

Project Files

//greeter.ixx
export module greeter;

import <iostream>;
import <string>;

using namespace std;

string message(string name) {
	return "Hello, " + name;
}

export void Greeter(string name) {
	cout << message(name) + "\n";
}
// helloworld.cpp
import <iostream>;
import greeter;

using namespace std;

int main()
{
    cout << "Hello, World!\n";

    Greeter("World!");
}

Done. Continue if you want to use Microsoft Experimental STL Modules.


Microsoft Experimental STL Modules

Importing import std.core

// helloworld.cpp
import std.core;

using namespace std;

int main()
{
    cout << "Hello, World!\n";
}

Errors and Project Properties to clear the errors.

These can be hard to find in the properties settings, go to C/C++ -> All Options and search for the settings.

Error:

fatal error C1011: cannot locate standard module interface. Did you install the library part of the C++ modules feature in VS setup?

Fix: Enable Experimental C++ Standard Library Modules

Error:

warning C5050: Possible incompatible environment while importing module ‘std.core’: _GUARDOVERFLOW_CRT_ALLOCATORS=1 is defined in current command line and not in module command line

Fix: SDL checks -> No (/sdl-)

Error:

warning C5050: Possible incompatible environment while importing module ‘std.core’: _M_FP_PRECISE is defined in current command line and not in module command line

Fix: Floating Point Model -> delete text, leave blank

Error:

warning C5050: Possible incompatible environment while importing module ‘std.core’: mismatched C++ versions. Current

Fix: C++ Language Standard -> Preview /std:c++latest

Error.

warning C5050: Possible incompatible environment while importing module ‘std.core’: _DEBUG is defined in current command line and not in module command line

There are two options.

  1. Always compile in Release, or
  2. remove _DEBUG; from Preprocessor Definitions in Project Properties

Final Project Properties for importing std.core

All Configurations:

  • C/C++ -> All Options
  • C++ Language Standard -> Preview /std:c++latest
  • Enable Experimental C++ Standard Library Modules
  • SDL checks -> No (/sdl-)
  • Floating Point Model -> delete text, leave blank
  • Scan Sources for Module Dependencies -> Yes

Debug Configuration:

Preprocessor Definitions -> Remove _DEBUG; or always compile in Release mode.