-1

I would like to use C++'s std::format library to format strings. See the minimal working example below.

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

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

However, when I compile my code, I get the following error message:

example.cpp:2:10: fatal error: format: No such file or directory
    2 | #include <format>

I am using the latest version of macOS, and I have Homebrew installed as my package manager. I already installed clang-format through Homebrew, but for some reason, my compiler can't locate the header file. Can somebody help me figure out the problem is? I have tried using Apple's GCC and the custom GCC10 provided by Homebrew, but in both cases, I get the same error message. Is this a Homebrew issue or a C++ issue?

Erick Ruiz
  • 41
  • 3
  • 1
    What command do you use to compile? – Joseph Larson Feb 02 '21 at 20:58
  • 4
    FYI `clang-format` is a code formatting CLI tool, and is not related to ``/`std::format`. The status of format support in libc++ is listed as "in progress" at https://libcxx.llvm.org/docs/Cxx2aStatus.html, so I'm not sure if it is available (if so, possibly under the `experimental` namespace). In the meantime you can use [`fmt`](https://github.com/fmtlib/fmt) instead. – jtbandes Feb 02 '21 at 20:59
  • 1
    It looks like this is part of C++ 20. Is that even available on your Mac yet? – Joseph Larson Feb 02 '21 at 21:01
  • Yes, at a minimum you would need to use something like `-std=c++2a` for this to work. – jtbandes Feb 02 '21 at 21:15
  • @jtbandes Is the only one with correct/helpful information so far. And I'm not sure anything else needs to be said. – sweenish Feb 02 '21 at 22:10
  • @JosephLarson I am using the following command to compile: `g++-10 -Wall -std=c++11 example.cpp -o example`. I have also tried using Apple's Clang version of GCC, but I still get an error message saying that the header file can't be found. Could this be a Homebrew issue? – Erick Ruiz Feb 02 '21 at 23:42

1 Answers1

-2

Following this answer to use fmt, you can install the library with brew install fmt, then modified the code to

/* 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;
}

and compile with

clang++ -std=c++11 test.cpp -lfmt
Yang Yushi
  • 725
  • 4
  • 20
  • 1
    Don't repeat answers, flag as duplicate and move on. The C++20 flag is not needed, and neither is the v7 namespace. If you brew installed fmt, indicating a local header is subjectively less than ideal was well. – sweenish Feb 02 '21 at 22:15
  • The answer indicated to use `fmt` and I demonstrated the way to make it work on a Mac. The code won't compile without the c++20 flag. The namespace is a good point though, thank you! – Yang Yushi Feb 02 '21 at 22:17
  • 1
    That's a lie. I use fmt on a mac, and have never compiled to C++20. – sweenish Feb 02 '21 at 22:24
  • Oh yes actually the c++11 would be enough. Thank you – Yang Yushi Feb 02 '21 at 22:25