gpt4 book ai didi

c++ - 链接错误与 fmt : undefined reference to `std::string fmt::v6::internal::grouping_impl(fmt::v6::internal::locale_ref)'

转载 作者:行者123 更新时间:2023-12-01 14:52:48 29 4
gpt4 key购买 nike

在我们的项目中,我们决定在我们的项目中使用最新的 fmt 版本 (6.2.0) 并主要使用 printf 功能,因为我们在广泛使用 printf 的地方进行日志记录。

我使用 fmt 包中包含的 CMakeLists.txt 在我们的 Linux 机器上构建了 libfmt.a。在我的过程中,我包含了 libfmt include 目录和 target_link_libraries。在代码中我只使用了 #include<fmt/printf.h> .现在当我编译代码时,代码被编译但是在链接时,我得到错误:还有很多但以下是第一个,我相信如果这个问题得到解决,其余的将自动解决

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)'

我做了一些分析,发现这个函数的定义存在于 format-inl.h 中。我尝试将它包含在我的代码中,但仍然存在相同的链接问题。

现在,当我在代码中定义宏 FMT_HEADER_ONLY 时,链接起作用了。

我的问题是:当我链接库 libfmt.a 时,它找不到该函数。为什么?我不想使用仅 header 版本。

请告诉我如何解决这个问题。

最佳答案

链接顺序很重要(参见 https://stackoverflow.com/a/409470/471164 )。确保在使用它的源文件或目标文件之后传递库。例如:

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

此示例从 github 获取 {fmt},编译它,创建一个小的测试源,编译并运行它给出以下输出:

Hello, world!

如果您在 test.cc 之前传递 -lfmt,您将遇到链接错误。

我建议使用 CMake,它将为您处理链接顺序:https://fmt.dev/latest/usage.html#usage-with-cmake

关于c++ - 链接错误与 fmt : undefined reference to `std::string fmt::v6::internal::grouping_impl<char>(fmt::v6::internal::locale_ref)' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61594396/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com