gpt4 book ai didi

c++ - 如何在 GCC 中使用 c++20 模块?

转载 作者:行者123 更新时间:2023-12-01 14:37:16 25 4
gpt4 key购买 nike

我目前正在尝试名为 的新 C++20 功能。模块 如所述here使用 GCC 10.1.0,但是如果我尝试构建以下代码片段,编译器会抛出一堆错误。
这是我到目前为止写的片段:

// 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。

关于c++ - 如何在 GCC 中使用 c++20 模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62765630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com