格式化没有这样的文件或目录
我试图使用 C++ 格式实用程序 (std::format)。我试图编译这个简单的程序:
#include <format>
int main()
{
std::cout << std::format("{}, {}", "Hello world", 123) << std::endl;
return 0;
}
当我尝试用 编译时g++ -std=c++2a format_test.cpp,它给了我这个:
format_test.cpp:1:10: fatal error: format: No such file or directory
1 | #include <format>
|
我有 GCC 10.2.0
回答
根据这个:https : //en.cppreference.com/w/cpp/compiler_support目前没有支持“文本格式”(P0645R10,std::format)的编译器。(截至 2020 年 12 月)
该论文定义的功能测试宏是__cpp_lib_format (也在此处列出),因此您可以编写这样的代码来检查:
#if __has_include(<format>)
#include <format>
#endif
#ifdef __cpp_lib_format
// Code with std::format
#else
// Code without std::format, or just #error if you only
// want to support compilers and standard libraries with std::format
#endif
该提案还链接到https://github.com/fmtlib/fmt作为完整实现,fmt::format而不是std::format. 尽管您必须跳过一些麻烦来链接依赖项或将其添加到您的构建系统,并在必要时处理许可/确认。
你的例子{fmt}:https : //godbolt.org/z/Ycd7K5