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

e5nqia27  于 2023-03-25  发布在  其他
关注(0)|答案(4)|浏览(321)

我目前正在尝试使用GCC 10.1.0的here所描述的新的C++20特性,称为模块,但是如果我试图构建下面的代码片段,编译器会给我一堆错误。
这是我到目前为止写的片段:

// 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();
      |     ^~~~~

我做错了什么?

ryevplcw

ryevplcw1#

GCC's language status page说它还不支持模块。
C20的支持还不完整(考虑到我们已经2020年了,这是公平的!而且C20技术上还不存在...)。
但是,通过一些标志和一个开发分支,您可以尝试正在进行的实现-在GCC's Modules Wiki上阅读更多关于它的信息。
GCC 10中的默认语言版本是C14;GCC 11提升到C17。

tez616oj

tez616oj2#

我看到GNU gcc网站23.1.21,它说你必须包括一个名为-fmodules-ts的标志。

kyvafyod

kyvafyod3#

现在,如果你使用GCC,完成:

# g++ -std=c++20 -fmodules-ts hello.cpp -o hello.exe -x c++-system-header iostream

来源:https://stackoverflow.com/a/73643558/19946116
如果你更喜欢Visual Studio 2022,你需要配置它:https://stackoverflow.com/a/73643523/19946116 .

5m1hhzi4

5m1hhzi44#

有了你的两个线人更好的办法是
g++ -std=c++20 -fmodules-ts hello-m.cpp main.cpp -o hello -xc++-system-header iostream
使用GCC 12.2.1。

相关问题