Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format no such file or directory

Tags:

c++

gcc

fmt

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

like image 745
cael ras Avatar asked Dec 31 '22 18:12

cael ras


1 Answers

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

like image 155
Artyer Avatar answered Jan 12 '23 15:01

Artyer