如何解决C++-module 导入链导致奇怪的编译器错误
当用 clang 编译 c++-20 模块时。导入一个导入另一个文件的文件时出现奇怪的编译错误,该文件导入了第三个文件,如下所示:
// a.cppm
#include <string>
export module a;
export std::string getStuffA() {
return "a";
}
// b.cppm
#include <string>
import a;
export module b;
export std::string getStuffB() {
return "b" + getStuffA();
}
// c.cpp
#include <string>
import b;
int main() {
std::cout << getStuffB() << std::endl;
return 0;
}
编译器输出:
In file included from ./src/c.cpp:1:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/iostream:39:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/ostream:38:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/ios:44:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/basic_ios.h:37:
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/locale_facets.h:2569:5: error: inline declaration of 'isspace' follows non-inline definition
它还列出了一些似乎同时具有内联和非内联定义的函数,例如 isprint、iscntrl、isupper。
有什么办法可以解决这些错误。
注意:删除std::string
和#include <string>
会删除编译错误,所以我认为错误来自<string>
解决方法
如前所述,gcc 11 支持模块,并且您正在尝试将 clang 与 gcc 10 标准 C++ 库一起使用。
使用适当的 C++ 标准库后,预处理器包含指令(例如 #include <string>
)将自动映射到 import <string>;
。使用不支持模块的 gcc 10 标准 C++ 库时,您会遇到诸如此类的错误。
如果您使用支持模块的 clang,并且您似乎正在使用这种现代 clang,否则您会在导入指令中遇到另一个错误,您应该使用 clang C++ 标准库:-stdlib=libc++
。>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。