Format no such file or directory

16.5k Views Asked by At

I was trying to use the C++ format utility (std::format). I tried to compile this simple program:

#include <format>

int main()
{
   std::cout << std::format("{}, {}", "Hello world", 123) << std::endl;

   return 0;
}

When I try compiling with g++ -std=c++2a format_test.cpp, it gives me this:

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

I have got GCC 10.2.0

3

There are 3 best solutions below

0
On BEST ANSWER

Finally, std::format is now supported in gcc 13.1 version! https://godbolt.org/z/TW9G8Gh5Y

4
On

According to this: https://en.cppreference.com/w/cpp/compiler_support there are currently no compilers that support "Text formatting" (P0645R10, std::format). (As of December 2020)

The feature test macro defined by that paper is __cpp_lib_format (also listed here), so you can write your code like this to check:

#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

The proposal also links to https://github.com/fmtlib/fmt as a full implementation, with fmt::format instead of std::format. Though you have to jump through some hoops to link the dependency or add it to your build system and to deal with the license / acknowledgement if necessary.

Your example with {fmt}: https://godbolt.org/z/Ycd7K5

1
On

Text formating P0645R10 is implemented in

  • GCC libstdc++ 13
  • Clang libc++ 14 (but marked as incomplete)
  • MSVC STL 19.29 (VS 2019 16.10)

Source