我正在尝试运行一个简单的程序:
\nimport <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我尝试过运行:
\ng++ -std=gnu++20 -o hello hello.cpp \n
Run Code Online (Sandbox Code Playgroud)\n或者
\ngcc -std=c++20 -o hello hello.cpp \n
Run Code Online (Sandbox Code Playgroud)\n但他们俩都给了我
\nhello.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现在我明白了
\nIn 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我目前被困在这里...
\nste*_*ver 23
截至发布之日,g++ 中的模块支持尚未完成。尤其,
\n\n\n标准库头单元
\n标准库不作为可导入的标头单元提供。如果要导入此类单元,则必须首先显式构建它们。\n 如果您不小心执行此操作,则可能有多个声明,模块机器必须合并这些声明。\nxe2\x80\x94 编译器资源\n的使用可能会受到将头文件划分为头单元的方式的影响。
\n
您可以使用构建 iostream 模块
\ng++ -std=c++20 -fmodules-ts -xc++-system-header iostream\n
Run Code Online (Sandbox Code Playgroud)\n这会gcm.cache
在当前目录中创建一个目录,内容如下
$ 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
编译器标志构建你的:
$ 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 归档时间: |
|
查看次数: |
11549 次 |
最近记录: |