點擊 <C 語言編程核心突破> 快速C語言入門
VS2022使用modules
- 前言
- 一、準備
- 二、使用
- 其一, 用VS installer 安裝模塊:
- 第二個選項就是, 與你的代碼一同編譯std模塊, 這個非常簡單, 但是也有坑.
- 總結
前言
要解決問題: 使用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_MODULEmodule;// 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語言入門