如何使用 g++ 成功编译任何东西,启用模块?

问题描述 投票:0回答:1

我有以下文件:

文件Testing2.cpp

#include <string>
#include <iostream>

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

当我这样编译它时:

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
时和未启用时的编译方式不同?

c++ gcc g++ c++20 c++-modules
1个回答
3
投票

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

#include <string>
import <string>;
.

我的问题是通过从 gcm.cache 目录中删除 string 和 iostream 模块并重新编译它们来解决的(我使用了命令

g++-11 -std=c++20 -fmodules-ts -c -x c++-system-header string
)。

© www.soinside.com 2019 - 2024. All rights reserved.