【发布时间】:2021-03-30 17:10:21
【问题描述】:
我刚刚购买了《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.
gcc 的版本是: g++ (GCC) 10.0.0 20200110(实验性)[svn-280157:20201220-1704] 我在 Stack Overflow 上发现了一个帖子,有人指向这个版本 (How to compile C++ code using modules-ts and gcc (experimental)?)
我还尝试了 wiki 中的示例(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) 版本?)
【问题讨论】:
-
如果你想学习模块,你会被塞满的。如果您是尝试学习一些 C++ 的新手,那么继续学习的实用方法是切换回旧的包含语法
include <iostream>:) -
我是一个尝试学习 C++ 的新手,我从 import
;方法被认为是与 #include 方法相比的“更好”解决方案。我不妨开始使用更新/更好的方法来做到这一点。但我想我现在只需要使用#include 方法。感谢您的回复! -
我完全同意,但我只看到了使用 MSVC 编译器的模块的工作实现。您可能需要等待一段时间才能在 GCC 或 Clang 中实现它。