目前不确定是否支持使用 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.
g++ -std=c++20 -fmodules-ts -c -x c++-system-header numbers
编译头文件,生成 CMI/BMI 文件(需要手动)。不幸的是,g++ 目前还不支持字符串格式化库。 - Desmond Gold