简体   繁体   English

使用 fmt 链接错误:未定义对 `std::string fmt::v6::internal::grouping_impl 的引用<char> (fmt::v6::internal::locale_ref)'</char>

[英]Linking errors with fmt: undefined reference to `std::string fmt::v6::internal::grouping_impl<char>(fmt::v6::internal::locale_ref)'

In our project, we decided to use the latest fmt version(6.2.0) in our project and use mainly the printf functionality as we had our logging where we are using printf extensively.在我们的项目中,我们决定在我们的项目中使用最新的 fmt 版本(6.2.0)并主要使用 printf 功能,因为我们在日志中广泛使用 printf。

I built the libfmt.a on our Linux box using the CMakeLists.txt included in the fmt package.我使用 fmt package 中包含的 CMakeLists.txt 在我们的 Linux 框中构建了 libfmt.a。 In my process, I included the libfmt include directory and in the target_link_libraries.在我的过程中,我包含了 libfmt 包含目录和 target_link_libraries。 In the code I only used #include<fmt/printf.h> .在代码中我只使用了#include<fmt/printf.h> Now when I compile the code, the code gets compiled but at the time of linking, I get the errors: There are many more but the following is the first one and I believe if this gets resolved, rest will be resolved automatically现在当我编译代码时,代码被编译,但在链接时,我得到错误:还有很多,但以下是第一个,我相信如果这个问题得到解决,rest 将自动解决

abc.cpp:(.text._ZN3fmt2V68internal8groupingIcEESsNS1_10locale_refE[_ZN3fmt2v68internal8groupingIcEESsNS1_10locale_refE]+0X20): undefined eference to `std::string fmt::v6::internal::grouping_impl(fmt::v6::internal::locale_ref)' abc.cpp:(.text._ZN3fmt2V68internal8groupingIcEESsNS1_10locale_refE[_ZN3fmt2v68internal8groupingIcEESsNS1_10locale_refE]+0X20): 对`std::string fmt::v6::internal::grouping_impl(fmtref::v6::internal::locale)的未定义引用

I did some analysis and found that this function's definition is present in format-inl.h.我做了一些分析,发现这个函数的定义存在于format-inl.h中。 I tried including it in my code but still the same linking issue.我尝试将它包含在我的代码中,但仍然存在相同的链接问题。

Now, when I defined the macro FMT_HEADER_ONLY in the code the linking worked.现在,当我在代码中定义宏FMT_HEADER_ONLY时,链接起作用了。

My question is: When I am linking with the library libfmt.a, its not able to find that function.我的问题是:当我与库 libfmt.a 链接时,它无法找到 function。 Why?为什么? I do not want to use the header-only version.我不想使用仅标题版本。

Please let me know how to get this fixed.请让我知道如何解决这个问题。

The linkage order is important (see https://stackoverflow.com/a/409470/471164 ).链接顺序很重要(参见https://stackoverflow.com/a/409470/471164 )。 Make sure to pass the library after the source or object files that use it.确保在使用它的源文件或 object 文件之后传递库。 For example:例如:

git clone https://github.com/fmtlib/fmt.git
cd fmt
make fmt
cat <<EOF > test.cc
#include <fmt/printf.h>

int main() {
  fmt::printf("Hello, %s!", "world");
}
EOF
g++ test.cc -L. -lfmt -Iinclude
./a.out

This example fetches {fmt} from github, compiles it, creates and a small test source, compiles and runs it giving the following output:这个例子从 github 获取 {fmt},编译它,创建一个小的测试源,编译并运行它,给出以下 output:

Hello, world!

If you pass -lfmt before test.cc you'll get linkage errors.如果你在-lfmt之前通过test.cc你会得到链接错误。

I recommend using CMake which will take care of the linkage order for you: https://fmt.dev/latest/usage.html#usage-with-cmake我建议使用 CMake 它将为您处理链接顺序: https://fmt.dev/latest/usage.html#usage-with-cmake

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

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