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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With