I've just purchased the book Beginning C++20 (the eBook version) and I'm trying to compile the first example using the new C++20 method.
The content of the source file is
// Ex1_01.cpp
// A Complete C++ program
import <iostream>;
int main()
{
int answer{42}; // Defines answer with 42
std::cout << "The answer to life, the universe, and everything is "
<< answer
<< std::endl;
return 0;
}
If I understand correctly this is not yet supported by GCC version 10 or 11 (some sites claim that GCC 11 support it, but when I use the -fmodules-ts flag as some suggest there is an error message that it's not implemented/experimental and quits.
After some searching I've found some posts refering to https://gcc.gnu.org/wiki/cxx-modules where there are instructions to install a version of GCC 10 with support for modules (using the -fmodules-ts flag) but when I use that on my example code I get the following error:
In module imported at Ex1_01.cpp:3:1:
/usr/local/include/c++/10.0.0/iostream: error: failed to read compiled module: No such file or directory
/usr/local/include/c++/10.0.0/iostream: note: compiled module file is ‘gcm.cache/./usr/local/include/c++/10.0.0/iostream.gcm’
/usr/local/include/c++/10.0.0/iostream: fatal error: jumping off the crazy train to crashville
compilation terminated.
The version of gcc is: g++ (GCC) 10.0.0 20200110 (experimental) [svn-280157:20201220-1704] I found a post here on Stack Overflow where someone points to this version (How to compile C++ code using modules-ts and gcc (experimental)?)
I also tried the examples from the wiki (hello.cc and main.cc) but those also give an error message:
In module imported at main.cpp:1:1:
hello: error: failed to read compiled module: No such file or directory
hello: note: compiled module file is ‘gcm.cache/hello.gcm’
hello: fatal error: jumping off the crazy train to crashville
compilation terminated.
Is there a way to make this work, or should I just start with the "old" #include method until there is a stable release of GCC 11 with support for modules? As I understand if I build the latest snapshot of GCC 11 most other C++20 specific code should work? (or just stick with the default (g++ (Debian 10.2.1-1) 10.2.1 20201207) version supplied by my distribution?)
I guess I'll be answering my own question.
When I follow the instructions on the GCC wiki (https://gcc.gnu.org/wiki/cxx-modules) it's a newer version compared to the one found on svn.
The svn has a version 10 of gcc while github has version 11.
When I compile the version for github (g++ (GCC) 11.0.0 20201217 (experimental) [c++-modules revision 20201220-2203]) the examples provided by the GCC Wiki compile and work. These files are hello.cpp:
module;
#include <iostream>
#include <string_view>
export module hello;
export void greeter (std::string_view const &name)
{
std::cout << "Hello " << name << "!\n";
}
and main.cpp
import hello;
int main (void)
{
greeter ("world");
return 0;
}
The command to compile is: g++ -fmodules-ts hello.cpp main.cpp
As I understand the order of the sourcefiles is important so hello.cpp needs to be compiled before main.cpp
So at the moment it seems that only user-made modules work, but not the Standard library ones (for those #include is still required).
[edit] It seems that the modules support is now merged with the main branch of gcc-11 so using the developer builds through git or svn are no longer required (unfortunately the standard library headers like are not converted into modules yet, so at the moment import ; doesn't work).
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