MSVC++ 编译 module std

环境: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 the import 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. Add std.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.cpp

import 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

  • 21
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值