【问题标题】:Where does Homebrew install C++ packages?Homebrew 在哪里安装 C++ 包?
【发布时间】: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 工具,与 &lt;format&gt;/std::format 无关。 libc++ 中格式支持的状态在libcxx.llvm.org/docs/Cxx2aStatus.html 列为“进行中”,所以我不确定它是否可用(如果有,可能在experimental 命名空间下)。与此同时,您可以改用fmt
  • 这看起来像是 C++ 20 的一部分。你的 Mac 上还可以使用吗?
  • 是的,您至少需要使用-std=c++2a 之类的东西才能使其工作。
  • @jtbandes 是迄今为止唯一具有正确/有用信息的人。我不确定还有什么需要说的。

标签: c++ macos homebrew


【解决方案1】:

按照this answer使用fmt,可以用brew install fmt安装库,然后修改代码为

/* example.cpp */
#include "fmt/format.h"
#include <iostream>
#include <string>

int main() {
   std::string s = fmt::format("Hello, {}!", "John");
   std::cout << s << std::endl;
   return 0;
}

并编译

clang++ -std=c++11 test.cpp -lfmt

【讨论】:

  • 不要重复答案,标记为重复并继续。不需要 C++20 标志,v7 命名空间也不需要。如果你 brew 安装了 fmt,表明本地头在主观上不太理想。
  • 答案表明使用fmt,我演示了使其在Mac上工作的方法。如果没有 c++20 标志,代码将无法编译。命名空间是个好点,谢谢!
  • 那是谎言。我在 mac 上使用 fmt,从未编译到 C++20。
  • 哦,是的,实际上 c++11 就足够了。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 2011-08-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多