我有以下文件:
#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
时和未启用时的编译方式不同?
事实证明,在这两种情况下,如果可用(位于 gcm.cache 文件夹中),g++ 将使用预编译的标准库模块:
#include <string>
和 import <string>;
.
我的问题是通过从 gcm.cache 目录中删除 string 和 iostream 模块并重新编译它们来解决的(我使用了命令
g++-11 -std=c++20 -fmodules-ts -c -x c++-system-header string
)。