Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use c++20 modules with GCC?

Tags:

c++

c++20

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?

like image 461
beep Avatar asked Jul 06 '20 22:07

beep


People also ask

Is C++20 supported on GCC?

C++20 Support in GCC GCC has experimental support for the latest revision of the C++ standard, which was published in 2020. C++20 features are available since GCC 8.

Does GCC support modules?

C++ Modules. A module system is coming to C++20, this page describes the GCC implementation state. A module system will provide several benefits: Reduce build times due to not reparsing large header files.

Are modules in C++20?

C++20 introduces modules, a modern solution that turns C++ libraries and programs into components. A module is a set of source code files that are compiled independently of the translation units that import them.

Does CMake support C++ modules?

CMake currently does not support C++20 modules. See also the relevant issue in the CMake issue tracker. Note that supporting modules requires far more support from the build system than inserting a new compiler option.


1 Answers

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.

like image 106
Asteroids With Wings Avatar answered Sep 28 '22 01:09

Asteroids With Wings