导入模块的标准方式 [英] Standard way of importing modules
问题描述
我目前正在尝试在应同时在Windows(MSVC)和Linux(Clang和/或GCC)上编译的代码中使用c ++模块.
I am currently trying to use c++ modules in a code that should compile both on Windows (MSVC) and Linux (Clang and/or GCC).
我目前正在Visual Studio中进行开发,并使用标准符合模式"(/permissive-)使我的代码尽可能可移植.
I am currently developping in Visual Studio and used the "Standard Conformance Mode" (/permissive-) to make my code as portable as possible.
但是以下代码:
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;
}
不能使用/permissive-标志进行编译.我收到以下错误:
Can not compile with the /permissive- flag. I get the following error:
E3223没有找到要导入的模块文件"std.core"
E3223 Cound not find module file "std.core" for import
错误C2664:'int _CrtDbgReport(int,const char *,int,const char*,const char *,...)':无法将参数4从'int'转换为'const char *'
error C2664: 'int _CrtDbgReport(int,const char *,int,const char *,const char *,...)': cannot convert argument 4 from 'int' to 'const char *'
我tought"std.core"可能是仅用于Windows的东西,因此我尝试了以下操作(我在许多示例中都看到了它):
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>;
但是会导致以下错误:
错误C7612:找不到以下文件的标头单元'PATH_TO_VS \ include \ iostream'
error C7612: could not find header unit for 'PATH_TO_VS\include\iostream'
错误C7612:找不到以下文件的标头单元'PATH_TO_VS \ include \ vector'
error C7612: could not find header unit for 'PATH_TO_VS\include\vector'
错误C7612:找不到"PATH_TO_VS \ include \ map"的标头单元
error C7612: could not find header unit for 'PATH_TO_VS\include\map'
注意:在PATH_TO_VS \ include中,实际上有名为"iostream","vector"和"map"的文件.
Note : There are actually files named "iostream", "vector", and "map" in PATH_TO_VS\include.
因此,我想知道导入c ++模块的标准方法是什么?如果"import std.core"是标准方式,为什么不使用/permissive-编译?
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- ?
我正在使用Visual Studio 2019(社区)和CMake.
I am using Visual Studio 2019 (Community) and CMake.
对不起,我忘了告诉我的编译器标志:
Sorry i forgot to tell my compiler flags:
/experimental:module
/std:c++latest
/W4
/WX
/permissive-
/MDd
/EHsc
代码编译时不带/permissive-,但设置时不编译.我不知道为什么
The code compiles without /permissive-, but does not when it is set. I can't figure out why
推荐答案
根据Microsoft Docs,导入标头尚未实现.请参阅 https://docs.microsoft.com/en-us/cpp/cpp/modules-cpp?view=msvc-160#imported-header-files .
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.
您可以在此处关注此功能的进度: https://github.com/microsoft/STL/issues/60 .
You can following the progress on this feature here: https://github.com/microsoft/STL/issues/60.
您可以在Visual Studio 2019中使用 import std.core;
语法(我在v16.8 +上对此进行了测试),但是您还需要为以下安装" C ++模块"v142构建工具"Visual Studio安装程序中的组件才能正常工作.
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
如此答案所述.
在导入 std.core
模块时,您可能仍会收到一些有关不兼容环境的 C5050
警告:
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
- 要解决第一个警告更改,请 SDL检查更改为否(
/sdl-
). - 要解决第二个警告,请删除
_DEBUG
预处理程序定义. - 要解决第三个警告,请删除浮点模型的值(在我的情况下,默认情况下设置为
/fp:percise
)./li>
- 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).
这篇关于导入模块的标准方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!