Search code examples
c++gccclangfmt

How to use library fmt with clang


My code is like

#include "fmt/compile.h"

int main() {
  using namespace fmt::literals;
  auto result = fmt::format("{}"_cf, FMT_VERSION);
  printf("%s", result.data());
}

It cannot compile with clang. The compilation result can be seen here

How can I make it work with clang. thx


Solution

  • Build of fmt depends on a bunch of macros. Here, build fails because operator () ""_cf is not defined in the clang version. If you look at the code you'll see it depends on macro FMT_USE_NONTYPE_TEMPLATE_ARGS for whatever reason.

    Probably godbolt at inclusion of fmt doesn't declare the macro to be true for clang.

    Regardless, if you simply add the line #define FMT_USE_NONTYPE_TEMPLATE_ARGS true before including fmt the code will compile on clang too. This is not recommended solution, the macro should be defined at build level, not directly in the code.