C ++模块导入链导致奇怪的编译器错误 [英] C++-module import chain results in strange compiler errors
问题描述
使用clang编译c ++-20模块时.导入文件时,导入另一个文件,然后再导入第三个文件时,出现奇怪的编译错误:
When compiling c++-20 modules with clang. I get strange compilation errors when importing a file that imports another file that imports a third file like so:
// 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.
And it lists some more functions that seems to have both inline and non-inline definiton, like isprint, iscntrl, isupper.
有什么办法可以解决这些错误.
Is there any way to get around these errors.
注意:删除 std :: string
和 #include< string>
会删除编译错误,因此我认为这些错误来自< string>
Note: Removing std::string
and #include <string>
removes the compilation errors, so I think that the errors comes from <string>
推荐答案
如前所述,gcc 11支持模块,并且您正在尝试将glang与gcc 10标准C ++库一起使用.
As already said, modules are supported in gcc 11, and you are trying to use clang with gcc 10 standard C++ library.
使用正确的C ++标准库,预处理器include指令(例如 #include< string>
)将自动映射到 import< string> ;;
.对于不支持模块的gcc 10使用标准的C ++库,您将得到诸如所得到的错误.
With using the proper C++ standard library, preprocessor include directives such as #include <string>
will be automatically mapped to import <string>;
. With using the standard C++ library for gcc 10, that does not support modules, you will get errors such as you have got.
如果使用支持模块的clang,并且似乎正在使用这种现代clang,否则在import指令上会遇到另一个错误,则应使用clang C ++标准库: -stdlib = libc ++
.
If you use clang, that supports modules, and it seems you are using such modern clang, otherwise you would get another errors on the import directive, you should use the clang C++ standard library: -stdlib=libc++
.
这篇关于C ++模块导入链导致奇怪的编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!