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?)