首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GCC如何使用c++20模块?

GCC如何使用c++20模块?
EN

Stack Overflow用户
提问于 2020-07-06 22:51:44
回答 3查看 18.1K关注 0票数 22

我目前正在尝试新的C++20的特性,如使用GCC 10.1.0描述的模块,但是如果我试图构建下面的代码片段,编译器会抛出一大堆错误。

这就是我到目前为止写的片段:

代码语言:javascript
复制
// helloworld.cpp
export module helloworld;  // module declaration
import <iostream>;         // import declaration
 
export void hello() {      // export declaration
    std::cout << "Hello world!\n";
}
代码语言:javascript
复制
// main.cpp
import helloworld;  // import declaration
 
int main() {
    hello();
}

我正在使用g++ helloworld.cpp main.cpp -std=c++20编译它。

编译器给了我这个错误:

代码语言:javascript
复制
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();
      |     ^~~~~

我做错什么了?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-07-06 22:53:24

GCC的语言状态页面说它还不支持模块。

C++20支持是不完整的(考虑到我们在2020年,这是足够公平的!)C++20技术上还不存在…)。

但是,使用一些标志和一个开发分支,您可以使用正在进行中的实现--在GCC模Wiki上阅读更多有关它的信息。

GCC 10的默认语言版本是C++14;GCC 11 up to C++17。

票数 23
EN

Stack Overflow用户

发布于 2021-01-23 04:14:45

我在23.1.21的时候看到了GNU gcc的网站,它说你必须包括一个名为-fmodules ts的旗帜。以下是该网站的链接,以获取其他信息https://gcc.gnu.org/wiki/cxx-modules

票数 11
EN

Stack Overflow用户

发布于 2022-09-08 04:02:16

现在,如果你用GCC的话,你就可以完成:

代码语言:javascript
复制
# g++ -std=c++20 -fmodules-ts hello.cpp -o hello.exe -x c++-system-header iostream

来自:https://stackoverflow.com/a/73643558/19946116

如果您更喜欢Visual 2022,则需要配置它:https://stackoverflow.com/a/73643523/19946116

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62765630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档