14

I am currently trying to use c++ modules in a code that should compile both on Windows (MSVC) and Linux (Clang and/or GCC).

I am currently developping in Visual Studio and used the "Standard Conformance Mode" (/permissive-) to make my code as portable as possible.

However the following code:

import std.core;
int main()
{
    std::cout << "Hello, World! haha" << std::endl;

    std::vector<int> myVec{4};

    std::map<std::string, size_t> myMap;

    return 0;
}

Can not compile with the /permissive- flag. I get the following error:

E3223 Cound not find module file "std.core" for import

error C2664: 'int _CrtDbgReport(int,const char *,int,const char *,const char *,...)': cannot convert argument 4 from 'int' to 'const char *'

I tought "std.core" might be a windows-only thing so i tried the following (i saw it in many examples) :

import <iostream>;
import <vector>;
import <map>;

But it results in the following errors:

error C7612: could not find header unit for 'PATH_TO_VS\include\iostream'

error C7612: could not find header unit for 'PATH_TO_VS\include\vector'

error C7612: could not find header unit for 'PATH_TO_VS\include\map'

Note : There are actually files named "iostream", "vector", and "map" in PATH_TO_VS\include.

Therefore i'm wondering what is the standard way of importing c++ modules ? If "import std.core" is the standard way, why doesn't it compile with /permissive- ?

I am using Visual Studio 2019 (Community) and CMake.

Edit:

Sorry i forgot to tell my compiler flags:

/experimental:module
/std:c++latest
/W4
/WX
/permissive-
/MDd
/EHsc

The code compiles without /permissive-, but does not when it is set. I can't figure out why

2
  • 2
    std.core isn't a Windows thing. Commented Dec 22, 2019 at 18:59
  • Only the import<> syntax is part of C++20 for the standard library. (User-written modules can be used via import foo;.) Commented Dec 22, 2019 at 22:12

2 Answers 2

16

According to Microsoft Docs, import headers are not yet implemented. See https://learn.microsoft.com/en-us/cpp/cpp/modules-cpp?view=msvc-160#imported-header-files.

You can following the progress on this feature here: https://github.com/microsoft/STL/issues/60.

You can use the import std.core; syntax in Visual Studio 2019 (I tested this with v16.8+) but you will also need to install the "C++ Modules for v142 build tools" component in the Visual Studio Installer for this to work.

In addition, you will need to enable the following flags:

  • /std:c++latest
  • /experimental:module

As stated in this answer.

You may still get some C5050 warnings about incompatible environment while importing the std.core module:

1>C:\Test\C++\Modules\main.cpp(1,16): 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
1>C:\Test\C++\Modules\main.cpp(1,16): warning C5050: Possible incompatible environment while importing module 'std.core': _DEBUG is defined in current command line and not in module command line
1>C:\Test\C++\Modules\main.cpp(1,16): 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
  1. To solve the first warning change SDL Checks to No (/sdl-).
  2. To solve the second warning, remove the _DEBUG preprocessor definition.
  3. To solve the third warning, delete the value of the Floating Point Model (which was, by default, set to /fp:precise in my case).
Sign up to request clarification or add additional context in comments.

1 Comment

I had a similar problem. Instead of disabling SDL Checks, I disabled the warnings with #pragma warning(disable: 5050) and #pragma warning(disable: 4996)
4

According to https://learn.microsoft.com/en-us/cpp/cpp/modules-cpp?view=vs-2019 you need to use compiler switches

  • /experimental:module
  • /std:c++latest
  • /EHsc
  • /MD

As well as configuring experimental modules support for the project.

4 Comments

Thanks for the reply. I'm using the flags you mentionned and installed the c++ modules support when configuring Visual Studio. Could you explain what you mean by " configuring experimental modules support for the project." ? I am new to programming with Visual Studio and since i'm using CMake i didn't have to configure anything more so far.
If you go read the link, it says "In a Visual Studio project, right-click the project node in Solution Explorer and choose Properties. Set the Configuration drop-down to All Configurations, then choose Configuration Properties > C/C++ > Language > Enable C++ Modules (experimental)."
Happy Xmas to you. Unfortunately i had to remove the flag /permissive-. I'll do whithout it :)
Don't forget also to actually install the c++ new module libraries or no compiler switches will save your soul :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.