简体   繁体   English

如何在 clang 中使用库 fmt

[英]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.它不能用clang编译。 The compilation result can be seen here编译结果可以看这里

How can I make it work with clang.我怎样才能使它与clang一起工作。 thx谢谢

Build of fmt depends on a bunch of macros. fmt 的构建依赖于一堆宏。 Here, build fails because operator () ""_cf is not defined in the clang version.这里,构建失败,因为在 clang 版本中没有定义operator () ""_cf If you look at the code you'll see it depends on macro FMT_USE_NONTYPE_TEMPLATE_ARGS for whatever reason.如果您查看代码,您会发现无论出于何种原因,它都取决于宏FMT_USE_NONTYPE_TEMPLATE_ARGS

Probably godbolt at inclusion of fmt doesn't declare the macro to be true for clang.可能包含 fmt 的godbolt并没有声明宏对于 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.无论如何,如果您只是在包含fmt之前添加行#define FMT_USE_NONTYPE_TEMPLATE_ARGS true ,则代码也将在 clang 上编译。 This is not recommended solution, the macro should be defined at build level, not directly in the code.这不是推荐的解决方案,宏应该在构建级别定义,而不是直接在代码中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM