2024-05-22 VS2022使用modules


点击 <C 语言编程核心突破> 快速C语言入门



前言

要解决问题: 使用VS2022开启modules.

想到的思路: 跟着官方文档整.

其它的补充: 挺麻烦, 完成后很好玩.


一、准备

**注意: 本教程需要 Visual Studio 2022 17.5 或更高版本.**

检查你的VS版本:
在这里插入图片描述
在这里插入图片描述
打开cmd终端, 以下内容需要命令行进行:

先创建一个目录存放标准模块, 比如:

E:\clangC++

在cmd中, 使其成为当前目录:

使用以下命令编译命名模块:std

E:\clangC++> cl /std:c++latest /EHsc /nologo /W4 /c "%VCToolsInstallDir%\modules\std.ixx"

注意, 以上命令一定会出错, 因为系统没有%VCToolsInstallDir%, 你要找, 比如我的就在:

E:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\modules

无论用什么查找命令, 找到他, 然后替换过去.

实在找不到, 把这个存成std.ixx, 放到你建立的要放标准模块的目录下, 然后将"%VCToolsInstallDir%\modules\std.ixx"换成"std.ixx":

// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

// This named module expects to be built with classic headers, not header units.
#define _BUILD_STD_MODULE

module;

// The subset of "C headers" [tab:c.headers] corresponding to
// the "C++ headers for C library facilities" [tab:headers.cpp.c]
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fenv.h>
#include <float.h>
#include <inttypes.h>
#include <limits.h>
#include <locale.h>
#include <math.h>
#include <setjmp.h>
#include <signal.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <uchar.h>
#include <wchar.h>
#include <wctype.h>

export module std;

#pragma warning(push)
#pragma warning(disable : 5244) // '#include <meow>' in the purview of module 'std' appears erroneous.

// "C++ library headers" [tab:headers.cpp]
#include <algorithm>
#if _HAS_STATIC_RTTI
#include <any>
#endif // _HAS_STATIC_RTTI
#include <array>
#include <atomic>
#include <barrier>
#include <bit>
#include <bitset>
#include <charconv>
#include <chrono>
#include <codecvt>
#include <compare>
#include <complex>
#include <concepts>
#include <condition_variable>
#include <coroutine>
#include <deque>
#include <exception>
#include <execution>
#include <expected>
#include <filesystem>
#include <format>
#include <forward_list>
#include <fstream>
#include <functional>
#include <future>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <latch>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <memory_resource>
#include <mutex>
#include <new>
#include <numbers>
#include <numeric>
#include <optional>
#include <ostream>
#include <queue>
#include <random>
#include <ranges>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <semaphore>
#include <set>
#include <shared_mutex>
#include <source_location>
#include <span>
#include <spanstream>
#include <sstream>
#include <stack>
#include <stacktrace>
#include <stdexcept>
#include <stop_token>
#include <streambuf>
#include <string>
#include <string_view>
#include <strstream>
#include <syncstream>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <variant>
#include <vector>
#include <version>

// "C++ headers for C library facilities" [tab:headers.cpp.c]
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfenv>
#include <cfloat>
#include <cinttypes>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cuchar>
#include <cwchar>
#include <cwctype>

#pragma warning(pop)

再次运行命令, 不出意外仍然报错:

E:\clangC++> cl /std:c++latest /EHsc /nologo /W4 /c "std.ixx"

没有包含路径集

找到VsDevCmd.bat这个文件, 通常在:

E:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools

把它拉进cmd命令行运行, 也就是建立路径集

然后运行模块命令, 在你的目录下会产生两个文件: std.ifc, std.obj 就OK了.

如法炮制, 建立全局C函数模块

E:\clangC++> cl /std:c++latest /EHsc /nologo /W4 /c "std.ixx" "std.compat.ixx"

又多了两个文件: std.compat.ifc, std.compat.obj

或者直接用Developer Command Prompt for VS 2022, 它直接将VS编译环境集成了:

在这里插入图片描述
在这里插入图片描述

如果想在vscode中调试VS编译的程序, 就需要从这个环境中的终端以code .命令启动vscode程序, 具体的内容比较多, 请参考Configure VS Code for Microsoft C++, 英文的, 说的比较明白.

二、使用

在使用时候, 我遇到了大麻烦, 就是无数的报错, 经过各种搜索, 发现, 问题很简单, 要使用模块, 那么编译命令必须与编译模块的命令是一致的, 如有更改, 则模块失效.

所以, 如果你想用刚才的模块编译, 必须使用命令行:

E:\clangC++> cl /c /std:c++latest /EHsc /nologo /W4 /reference "std=std.ifc" Example.cpp
link Example.obj std.obj

否则, 必须重新编译模块, 这是因为模块是二进制的状态, 改动编译命令后, 二进制不兼容, 这个无解.

当然, 对于std模块, 微软还算有良心, 你有两个选择,

其一, 用VS installer 安装模块:

在这里插入图片描述
然后通过

import std.core;

引入标准库的内容, 具体如下:

std.regex 提供标头 <regex> 的内容

std.filesystem 提供标头 <filesystem> 的内容

std.memory 提供标头 <memory> 的内容

std.threading 提供标头 <atomic><condition_variable><future><mutex><shared_mutex><thread> 的内容

std.core 提供 C++ 标准库中的任何其他内容

使用的时候要打开项目的C++标准和模块选项:
在这里插入图片描述
然后就可以愉快的编译了, 忘了说了, 我的VS使用模块后, 所有代码提示及补全都没有了, 是的, 都用不了了 !

import std.core;

int main()
{
    for (size_t i = 0; i < 5; i++)
    {
        std::cout << "hello";
    }

    return 0;
}

所以, 模块的路还很长.

第二个选项就是, 与你的代码一同编译std模块, 这个非常简单, 但是也有坑.

把这个文件std.ixx拷贝到你的项目目录中, 在编译时, 增加一个命令行:

在这里插入图片描述

这时, 编译器会在链接前生成模块, 并且由于编译命令完全一致, 所以就不太会出错了, 当然我还是碰到下面的小问题, 好在解决了:

要确定不要开优化. 开了出错的概率很大.
在这里插入图片描述

在编译完成后, 如果日后你要使用同样的编译命令, 则可以直接连接模块了, 通过下面的命令:

在这里插入图片描述
还有连接器:

在这里插入图片描述
完活, 收工 !

2024-05-24 升级VS到最新版本, 代码提示和补全功能又回来了, 人类, 总是这要, 那要, 还要, 然后有需求, 就有满足.


总结

对于模块的支持, VS算是比较靠前了, 然而, 还是有坑, 坑要一个一个的踩.

我只能帮你到这了, 剩下的路, 自己慢慢踩吧.


点击 <C 语言编程核心突破> 快速C语言入门


  • 12
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不停感叹的老林_<C 语言编程核心突破>

不打赏的人, 看完也学不会.

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值