首页 > 解决方案 > 如何使用 fmt 库而不获取“架构 x86_64 的未定义符号”

问题描述

我正在尝试在我的 c++ 项目中使用 fmt ( https://github.com/fmtlib/fmt ) 格式头库。

我在主文件顶部添加了核心头文件的路径,如下所示:

#include "../third_party/fmt/core.h"

但是当我尝试调用任何函数时:

string message = fmt::format("The answer is {}", 42);

我收到以下错误:

Undefined symbols for architecture x86_64:
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > fmt::v5::internal::vformat<char>(fmt::v5::basic_string_view<char>, fmt::v5::basic_format_args<fmt::v5::buffer_context<char>::type>)", referenced from:
      std::__1::basic_string<std::__1::enable_if<internal::is_string<char [17]>::value, fmt::v5::internal::char_t<char [17]>::type>::type, std::__1::char_traits<std::__1::enable_if<internal::is_string<char [17]>::value, fmt::v5::internal::char_t<char [17]>::type>::type>, std::__1::allocator<std::__1::enable_if<internal::is_string<char [17]>::value, fmt::v5::internal::char_t<char [17]>::type>::type> > fmt::v5::format<char [17], int>(char const (&) [17], int const&) in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [main] Error 1
make[1]: *** [CMakeFiles/main.dir/all] Error 2
make: *** [all] Error 2

我不确定如何使用它,因为这是我过去使用其他头库(例如 cxxopts)的方式。任何帮助,将不胜感激!

标签: c++fmt

解决方案


您应该与fmt库链接或使用可选的仅标头模式

例如,如果您有文件test.cc

#include <fmt/core.h>

int main() {
  fmt::print("The answer is {}.", 42);
}

你可以用 gcc 编译和链接它:

g++ -std=c++11 test.cc -lfmt

推荐阅读