c++ - 如何使用 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)的方式。任何帮助,将不胜感激!
解决方案
您应该与fmt
库链接或使用可选的仅标头模式。
例如,如果您有文件test.cc
:
#include <fmt/core.h>
int main() {
fmt::print("The answer is {}.", 42);
}
你可以用 gcc 编译和链接它:
g++ -std=c++11 test.cc -lfmt
推荐阅读
- node.js - 使用 VSCode 远程容器下载 vscode-server 时无法建立 TLS 连接
- amazon-web-services - 在 AWS 中创建 IAM 策略以保护根用户的 EC2 实例
- javascript - 如何获取 Windows 命令结果并在 chrome 浏览器中显示警报
- laravel - Laravel 验证:如果另一个字段可用,则需要两个字段中的任何一个
- javascript - 如何从 HTML 表单中收集所有值?
- angular - 未处理的承诺拒绝:this._next 不是函数:@angular/fire/messaging 中的区域
- python - python对象的自定义过滤功能
- html - 为什么弹性容器内的弹性项目需要最小宽度来封装弹性项目
- ruby-on-rails - 当用户已经通过身份验证时,在设计中运行守望者策略
- ruby - 如何在 ruby 脚本中从 Jenkins 的 changeSets 中访问值