GCC如何使用C++20模块?
我目前正在使用GCC 10.1.0尝试名为模块的C++20的新功能,如here所述,然而,如果我尝试构建以下代码片段,编译器会抛出一堆错误。
这是我到目前为止写的代码片段:
// helloworld.cpp
export module helloworld; // module declaration
import <iostream>; // import declaration
export void hello() { // export declaration
std::cout << "Hello world!
";
}
// main.cpp
import helloworld; // import declaration
int main() {
hello();
}
我正在使用g++ helloworld.cpp main.cpp -std=c++20
编译它。
编译器给了我这个错误:
helloworld.cpp:2:1: warning: keyword ‘export’ not implemented, and will be ignored
2 | export module helloworld; // module declaration
| ^~~~~~
helloworld.cpp:2:8: error: ‘module’ does not name a type
2 | export module helloworld; // module declaration
| ^~~~~~
helloworld.cpp:3:9: error: ‘iostream’ was not declared in this scope
3 | import <iostream>; // import declaration
| ^~~~~~~~
helloworld.cpp:3:9: error: ‘iostream’ was not declared in this scope
helloworld.cpp:3:9: error: ‘iostream’ was not declared in this scope
helloworld.cpp:3:9: error: ‘iostream’ was not declared in this scope
helloworld.cpp:3:9: error: ‘iostream’ was not declared in this scope
helloworld.cpp:3:9: error: ‘iostream’ was not declared in this scope
helloworld.cpp:3:9: error: ‘iostream’ was not declared in this scope
helloworld.cpp:3:9: error: ‘iostream’ was not declared in this scope
helloworld.cpp:3:9: error: ‘iostream’ was not declared in this scope
helloworld.cpp:3:1: error: ‘import’ does not name a type
3 | import <iostream>; // import declaration
| ^~~~~~
helloworld.cpp:5:1: warning: keyword ‘export’ not implemented, and will be ignored
5 | export void hello() { // export declaration
| ^~~~~~
helloworld.cpp: In function ‘void hello()’:
helloworld.cpp:6:10: error: ‘cout’ is not a member of ‘std’
6 | std::cout << "Hello world!
";
| ^~~~
helloworld.cpp:1:1: note: ‘std::cout’ is defined in header ‘<iostream>’; did you forget to ‘#include <iostream>’?
+++ |+#include <iostream>
1 | // helloworld.cpp
main.cpp:2:1: error: ‘import’ does not name a type
2 | import helloworld; // import declaration
| ^~~~~~
main.cpp: In function ‘int main()’:
main.cpp:5:5: error: ‘hello’ was not declared in this scope
5 | hello();
| ^~~~~
我做错了什么?
解决方案
GCC's language status page表示它还不支持模块。
C++20支持不完整(考虑到我们在2020年,这是很公平的!从技术上讲,C++20还不存在(…)。
但是,使用一些标志和一个开发分支,您可以尝试正在进行的实现-请阅读GCC's Modules Wiki上的更多信息。
GCC 10的默认语言版本是C++14;GCC 11的默认语言版本是C++17。
相关文章