【发布时间】:2021-02-02 20:55:03
【问题描述】:
我想使用 C++ 的std::format 库来格式化字符串。请参阅下面的最小工作示例。
/* example.cpp */
#include <iostream>
#include <format>
#include <string>
int main() {
std::string s = std::format("Hello, {}!", "John");
std::cout << s << std::endl;
return 0;
}
但是,当我编译我的代码时,我收到以下错误消息:
example.cpp:2:10: fatal error: format: No such file or directory
2 | #include <format>
我使用的是最新版本的 macOS,并安装了 Homebrew 作为我的包管理器。我已经通过 Homebrew 安装了clang-format,但是由于某种原因,我的编译器找不到头文件。有人可以帮我找出问题所在吗?我曾尝试使用 Apple 的 GCC 和 Homebrew 提供的自定义 GCC10,但在这两种情况下,我都会收到相同的错误消息。这是 Homebrew 问题还是 C++ 问题?
【问题讨论】:
-
你用什么命令编译?
-
仅供参考
clang-format是一个代码格式化 CLI 工具,与<format>/std::format无关。 libc++ 中格式支持的状态在libcxx.llvm.org/docs/Cxx2aStatus.html 列为“进行中”,所以我不确定它是否可用(如果有,可能在experimental命名空间下)。与此同时,您可以改用fmt。 -
这看起来像是 C++ 20 的一部分。你的 Mac 上还可以使用吗?
-
是的,您至少需要使用
-std=c++2a之类的东西才能使其工作。 -
@jtbandes 是迄今为止唯一具有正确/有用信息的人。我不确定还有什么需要说的。