GCC如何使用C++20模块?

How to use c++20 modules with GCC?(GCC如何使用C++20模块?)

本文介绍了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。

这篇关于GCC如何使用C++20模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:GCC如何使用C++20模块?

基础教程推荐