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

3

目前不确定是否支持使用 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添加。 2021年10月8日更新: 我创建了一个次要程序,删除了 <format>std :: format(),以测试使用-fmodules-ts标志实现import <numbers>;,但仍然无法工作。请参见以下程序和终端。
程序:
#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.

@sweenish,标志没有起作用,这就是为什么我发帖的原因,我认为我可能解释错误了。我不确定在gcc/g++ 11.1中是否支持<format>,所以我只使用数字运行了一个示例。请参见编辑。 - ExZerminator
4
请尝试使用命令 g++ -std=c++20 -fmodules-ts -c -x c++-system-header numbers 编译头文件,生成 CMI/BMI 文件(需要手动)。不幸的是,g++ 目前还不支持字符串格式化库。 - Desmond Gold
1
@DesmondGold 我是新手,所以我有点困惑如何使用它。你的意思是我必须先运行你建议的命令来编译头文件,然后再运行第二个命令来打开代码并编译?例如 'g++ randomCodeWhileRunning.cpp -o main -std=c++2a'(我还需要 '-fmodules-ts' 吗?)。谢谢你帮助一个新手 lol - ExZerminator
1
@DesmondGold,你的评论很有帮助。考虑将其作为答案添加进来。 - Sourav Kannantha B
1个回答

0

@ExZerminator,你是对的。已完成:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接