如何在 GCC 中使用 c++20 模块?
How to use c++20 modules with GCC?
我目前正在使用 GCC 10.1.0 尝试名为 modules 的新 C++20 功能 here,但是如果我尝试构建以下内容代码片段编译器抛出一堆错误。
这是我到目前为止写的片段:
// helloworld.cpp
export module helloworld; // module declaration
import <iostream>; // import declaration
export void hello() { // export declaration
std::cout << "Hello world!\n";
}
// 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!\n";
| ^~~~
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。
我在 23.1.21 看到了 GNU gcc 网站,它说你必须包含一个名为 -fmodules-ts 的标志。这是网站的 link 以获取更多信息
https://gcc.gnu.org/wiki/cxx-modules
我目前正在使用 GCC 10.1.0 尝试名为 modules 的新 C++20 功能 here,但是如果我尝试构建以下内容代码片段编译器抛出一堆错误。
这是我到目前为止写的片段:
// helloworld.cpp
export module helloworld; // module declaration
import <iostream>; // import declaration
export void hello() { // export declaration
std::cout << "Hello world!\n";
}
// 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!\n";
| ^~~~
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。
我在 23.1.21 看到了 GNU gcc 网站,它说你必须包含一个名为 -fmodules-ts 的标志。这是网站的 link 以获取更多信息 https://gcc.gnu.org/wiki/cxx-modules