在 Ubuntu 22.04 上编译使用模块的 C++20 程序

An *_*ea. 15 gcc c++

我正在尝试运行一个简单的程序:

\n
import <iostream>;\nint main()\n{\nstd::cout << "Hello, World!" << std::endl;\nreturn 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我已经检查过,据说我已经拥有最新版本的 gcc。\nbuild-essential 已经是最新版本(12.9ubuntu3)。

\n

我尝试过运行:

\n
g++ -std=gnu++20 -o hello hello.cpp \n
Run Code Online (Sandbox Code Playgroud)\n

或者

\n
gcc -std=c++20 -o hello hello.cpp \n
Run Code Online (Sandbox Code Playgroud)\n

但他们俩都给了我

\n
hello.cpp:1:9: error: \xe2\x80\x98iostream\xe2\x80\x99 was not declared in this scope\n    1 | import <iostream>;\n      |         ^~~~~~~~\nhello.cpp:1:9: error: \xe2\x80\x98iostream\xe2\x80\x99 was not declared in this scope\nhello.cpp:1:9: error: \xe2\x80\x98iostream\xe2\x80\x99 was not declared in this scope\nhello.cpp:1:9: error: \xe2\x80\x98iostream\xe2\x80\x99 was not declared in this scope\nhello.cpp:1:9: error: \xe2\x80\x98iostream\xe2\x80\x99 was not declared in this scope\nhello.cpp:1:9: error: \xe2\x80\x98iostream\xe2\x80\x99 was not declared in this scope\nhello.cpp:1:9: error: \xe2\x80\x98iostream\xe2\x80\x99 was not declared in this scope\nhello.cpp:1:9: error: \xe2\x80\x98iostream\xe2\x80\x99 was not declared in this scope\nhello.cpp:1:9: error: \xe2\x80\x98iostream\xe2\x80\x99 was not declared in this scope\nhello.cpp:1:1: error: \xe2\x80\x98import\xe2\x80\x99 does not name a type\n    1 | import <iostream>;\n      | ^~~~~~\nhello.cpp:1:1: note: C++20 \xe2\x80\x98import\xe2\x80\x99 only available with \xe2\x80\x98-fmodules-ts\xe2\x80\x99, which is not yet enabled with \xe2\x80\x98-std=c++20\xe2\x80\x99\nhello.cpp: In function \xe2\x80\x98int main()\xe2\x80\x99:\nhello.cpp:4:6: error: \xe2\x80\x98cout\xe2\x80\x99 is not a member of \xe2\x80\x98std\xe2\x80\x99\n    4 | std::cout << "Hello, World!" << std::endl;\n      |      ^~~~\nhello.cpp:1:1: note: \xe2\x80\x98std::cout\xe2\x80\x99 is defined in header \xe2\x80\x98<iostream>\xe2\x80\x99; did you forget to \xe2\x80\x98#include <iostream>\xe2\x80\x99?\n  +++ |+#include <iostream>\n    1 | import <iostream>;\nhello.cpp:4:38: error: \xe2\x80\x98endl\xe2\x80\x99 is not a member of \xe2\x80\x98std\xe2\x80\x99\n    4 | std::cout << "Hello, World!" << std::endl;\n      |                                      ^~~~\nhello.cpp:1:1: note: \xe2\x80\x98std::endl\xe2\x80\x99 is defined in header \xe2\x80\x98<ostream>\xe2\x80\x99; did you forget to \xe2\x80\x98#include <ostream>\xe2\x80\x99?\n  +++ |+#include <ostream>\n    1 | import <iostream>;\n
Run Code Online (Sandbox Code Playgroud)\n

然后我跑了:\ngcc -std=c++20 -fmodules-ts hello.cpp

\n

现在我明白了

\n
In module imported at hello.cpp:1:1:\n/usr/include/c++/11/iostream: error: failed to read compiled module: No such file or directory\n/usr/include/c++/11/iostream: note: compiled module file is \xe2\x80\x98gcm.cache/./usr/include/c++/11/iostream.gcm\xe2\x80\x99\n/usr/include/c++/11/iostream: note: imports must be built before being imported\n/usr/include/c++/11/iostream: fatal error: returning to the gate for a mechanical issue\ncompilation terminated.\n
Run Code Online (Sandbox Code Playgroud)\n

我目前被困在这里...

\n

ste*_*ver 23

截至发布之日,g++ 中的模块支持尚未完成。尤其,

\n
\n

标准库头单元

\n

标准库不作为可导入的标头单元提供。如果要导入此类单元,则必须首先显式构建它们。\n 如果您不小心执行此操作,则可能有多个声明,模块机器必须合并这些声明。\nxe2\x80\x94 编译器资源\n的使用可能会受到将头文件划分为头单元的方式的影响。

\n
\n

您可以使用构建 iostream 模块

\n
g++ -std=c++20 -fmodules-ts -xc++-system-header iostream\n
Run Code Online (Sandbox Code Playgroud)\n

这会gcm.cache在当前目录中创建一个目录,内容如下

\n
$ tree gcm.cache/\ngcm.cache/\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 usr\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 include\n        \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 c++\n            \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 11\n                \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 iostream.gcm\n\n4 directories, 1 file\n
Run Code Online (Sandbox Code Playgroud)\n

(我使用的是 Ubuntu 22.04 附带的默认 gcc/g++ 11.2.0-19ubuntu1)。

\n

然后你可以hello.cpp使用-fmodules-ts编译器标志构建你的:

\n
$ g++ -std=gnu++20 -fmodules-ts -o hello hello.cpp\n$ \n$ ./hello\nHello, World!\n
Run Code Online (Sandbox Code Playgroud)\n

参考:

\n\n

  • 这似乎使得新模块仅在当前位置可用,这可能将其限制为驻留在那里的源。是否有可能在系统范围内使用系统模块,也就是说,您可以“安装”它们吗?基本上,我们将构建 C++20 标准的这一部分并将其部署在本地。 (2认同)