如何使用 g++ 成功编译任何内容并启用模块?

Ade*_*art 7 c++ gcc g++ c++20 c++-modules

我有以下文件:

文件测试2.cpp

#include <string>
#include <iostream>

int main() {
    std::string ss = "aaaa";
    ss += "aa";
    std::cout << ss << "\n";
}
Run Code Online (Sandbox Code Playgroud)

当我像这样编译它时:g++-11 -o Testing2 Testing2.cpp -std=c++20,我得到了所需的输出。当我像这样编译它时:g++-11 -o Testing2 Testing2.cpp -std=c++20 -fmodules-ts,我收到运行时分段错误错误。

问题 1:这是 g++ 中的错误,还是我遗漏了什么?

我有几个类似的问题,一个与std::filesystem,一个与std::map。即使没有实际使用模块,代码也将无法正常工作。

问题2:上面的代码甚至没有使用模块。为什么 g++ 在-fmodules-ts启用和未启用时编译它的方式不同?

Ade*_*art 4

事实证明,在以下两种情况下,g++ 将使用预编译的标准库模块(如果可用)(位于 gcm.cache 文件夹中): #include <string>import <string>;

通过从 gcm.cache 目录中删除 string 和 iostream 模块并重新编译它们(我使用了命令g++-11 -std=c++20 -fmodules-ts -c -x c++-system-header string)解决了我的问题。