gpt4 book ai didi

c++ - 如何使用 gcc/g++ 11.1 导入模块

转载 作者:行者123 更新时间:2023-12-05 08:04:12 25 4
gpt4 key购买 nike

不确定 import <module_Name>;现在支持。当我尝试运行包含模块的程序时,它会显示 note: c++20 'import' only available with '-fmodules-ts'是像 -std=c++20 这样的编译器标志吗?或者是当前不支持的模块。这是一个使用模块的示例程序:

#include<iostream>
import <numbers>;
import <format>;

int main()
{
double pi {std::numbers::pi};
std::cout << std::format("Pi is = to {}", pi);

}

我知道我可以使用 #include <numbers>但我想弄清楚模块是否有效。我不太确定 <import>可以使用 #include 添加.

编辑 10/8/21:我制作了一个删除 <format> 的辅助程序和 std::format()测试 import <numbers>; 的执行情况使用 -fmodules-ts标志,它仍然没有工作。请参阅下面的程序和终端。

程序:

#include<iostream>
import <numbers>;


int main()
{
double pi {std::numbers::pi};
std::cout << pi;
}

终端:

g++ randomCodeWhileReading.cpp -o main -std=c++2a -fmodules-ts
randomCodeWhileReading.cpp:2:1: error: unknown Compiled Module Interface: no such module
2 | import <numbers>;
| ^~~~~~
In module imported at randomCodeWhileReading.cpp:2:1:
/usr/include/c++/11/numbers: error: failed to read compiled module: Unknown CMI mapping
/usr/include/c++/11/numbers: note: imports must be built before being imported
/usr/include/c++/11/numbers: fatal error: returning to the gate for a mechanical issue
compilation terminated.

最佳答案

@ExZerminator,你是对的。完成:

# g++ -std=c++20 -fmodules-ts hello.cpp -o hello.exe -x c++-system-header iostream

关于c++ - 如何使用 gcc/g++ 11.1 导入模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69502406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com