如何在仅标头模式下使用fmt库?

很难使用 fmt 库的仅标头模式。这是我详细尝试的内容:我从https://fmt.dev/latest/index.html下载了 fmt7.1.3,只将目录fmt-7.1.3/include/fmt放在目录 ( [trgdir]) 中并编写了一个 test.cpp 如下:

#include <iostream>
#include <fmt/format.h>
int main() {
    fmt::format("The answer is {}.", 42);
    return 0;
}

然后在我使用的终端中

gcc -I[trgdir] test.cpp

其中 gcc 我定义为

alias gcc='gcc-10 -xc++ -lstdc++ -shared-libgcc -std=c++17 -O2 '

我得到的错误是

Undefined symbols for architecture x86_64:
  "__ZN3fmt2v76detail7vformatB5cxx11ENS0_17basic_string_viewIcEENS0_11format_argsE", referenced from:
      _main in ccEeTo0w.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

我已经检查了这篇文章,但我仍然无法解决我的问题。如何在不获取“架构 x86_64 的未定义符号”的情况下使用 fmt 库

回答

您需要在包含之前定义一个宏,如下所示:

#define FMT_HEADER_ONLY
#include "fmt/format.h"

  • @Ethanabc You can find it at the very end of the `fmt/format.h` file. But, frankly saying, I don't understand why it's lacking in the official documentation.

以上是如何在仅标头模式下使用fmt库?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>