我正在尝试在我的 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
Run Code Online (Sandbox Code Playgroud)
我不确定如何使用它,因为这就是我过去使用其他标头库(例如 cxxopts)的方式。任何帮助,将不胜感激!
您应该链接到fmt库或使用可选的仅标头模式。
例如,如果您有以下文件test.cc:
#include <fmt/core.h>
int main() {
fmt::print("The answer is {}.", 42);
}
Run Code Online (Sandbox Code Playgroud)
您可以使用 gcc 编译并链接它:
g++ -std=c++11 test.cc -lfmt
Run Code Online (Sandbox Code Playgroud)