简体   繁体   中英

How to use c++20 modules with GCC?

I'm currently trying the new C++20's feature called modules as described here using GCC 10.1.0, however if i try to build the following snippet of code the compiler throw me a bunch of errors.

This is the snippets i wrote so far:

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

I'm compiling it using g++ helloworld.cpp main.cpp -std=c++20 .

The compiler gave me this error:

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

What i'm doing wrong?

GCC's language status page says it doesn't support modules yet.

C++20 support is not complete (which is fair enough given that we're in 2020. And C++20 technically doesn't exist yet…).

However, with some flags and a development branch you can play around with the in-progress implementation — read more about it on GCC's Modules Wiki .

The default language version in GCC 10 is C++14; GCC 11 ups that to C++17.

I saw the GNU gcc website as of 23.1.21 and it says u have to include a flag called -fmodules-ts. Here is the link for the website for additional info https://gcc.gnu.org/wiki/cxx-modules

For now, if you use GCC, done with:

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

Comes from: https://stackoverflow.com/a/73643558/19946116

And if you prefer Visual studio 2022, you need configure it: https://stackoverflow.com/a/73643523/19946116 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM