环境:windows 10 19045.xxxx
只安装了MSVC C++ 工具链和一个版本的SDK,SDK版本建议选一个和本机系统匹配的。
cd %USERPROFILE%\source\repos\STLModules
mkdir x86
mkdir x64
打开“x86 Native Tools Command Prompt for VS 2022”控制台,执行如下:
cd %USERPROFILE%\source\repos\STLModules\x86
cl /std:c++latest /EHsc /nologo /W4 /MTd /c "%VCToolsInstallDir%\modules\std.ixx"
再打开“x64 Native Tools Command Prompt for VS 2022”控制台,执行如下:
cd %USERPROFILE%\source\repos\STLModules\x64
cl /std:c++latest /EHsc /nologo /W4 /MTd /c "%VCToolsInstallDir%\modules\std.ixx"
此时在 x86, x64 目录下都生成两个文件:
这两个就是对应架构的std module文件
std.ifc
is the compiled binary representation of the named module interface that the compiler consults to process theimport std;
statement. This is a compile-time only artifact. It doesn't ship with your application.std.obj
contains the implementation of the named module. Addstd.obj
to the command line when you compile the sample app to statically link the functionality you use from the standard library into your application.
使用方法:
//example01.cppimport std;
int main()
{std::cout << "Import the STL library for best performance\n";std::vector<int> v{5, 5, 5};for (const auto& e : v){std::cout << e;}
}
以编译x86 exe为例,将%USERPROFILE%\source\repos\STLModules\x86 下的std.ifc和std.obj拷贝到源文件目录。
打开“x86 Native Tools Command Prompt for VS 2022”控制台,进入所在目录,执行:
cl /std:c++latest /EHsc /nologo /W4 /MTd example01.cpp std.obj
成功编译出example01.exe。
参考:Tutorial: Import the standard library (STL) using modules from the command line (C++) | Microsoft Learn