我刚购买了《Beginning C++20》这本书(电子版),现在我正在尝试使用新的C++20方法编译第一个示例。
源文件内容如下:
// 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;
}
如果我理解正确,GCC 10或11版本尚不支持此功能(一些网站声称GCC 11支持它,但当我像一些人建议的使用-fmodules-ts标志时,会出现错误消息表明它未实现/实验性并退出)。
经过一些搜索,我发现了一些帖子引用https://gcc.gnu.org/wiki/cxx-modules,其中有安装支持模块的GCC 10版本的说明(使用-fmodules-ts标志),但是当我在我的示例代码中使用它时,会出现以下错误:
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.
版本号为: g++ (GCC) 10.0.0 20200110 (experimental) [svn-280157:20201220-1704] 我在Stack Overflow上找到了一篇帖子,其中有人指向这个版本(如何使用modules-ts和gcc(实验版)编译C ++代码?) 我还尝试了维基百科上的示例(hello.cc和main.cc),但这些也会出现错误消息:
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.
有没有办法让这个工作起来,或者我应该使用“旧的”#include方法,直到GCC 11发布稳定版本并支持模块化?据我所知,如果我构建最新的GCC 11快照,大多数其他C ++20特定的代码应该可以工作?(或者只是使用由我的发行版提供的默认(g ++(Debian 10.2.1-1)10.2.1 20201207)版本?)
#include <iostream>
语法 :) - Mansoor