问题
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
回答1:
According to https://docs.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.
回答2:
According to Microsoft Docs, import headers are not yet implemented. See https://docs.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
- To solve the first warning change SDL Checks to No (
/sdl-
). - To solve the second warning, remove the
_DEBUG
preprocessor definition. - To solve the third warning, delete the value of the Floating Point Model (which was, by default, set to
/fp:percise
in my case).
来源:https://stackoverflow.com/questions/59447206/standard-way-of-importing-modules