You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用C++20编译含fmt/format.h的程序遇链接错误求助

解决fmt库链接错误问题

问题背景

编译一个使用C++20模块和fmt库的程序时,出现fmt相关的链接错误,无法生成可执行文件。

源代码

import <iostream>;
import <numbers>;
import <cmath>; 
//import <fmt/format.h>;
#include <fmt/format.h>

int main()
{
    // 2平方英尺池塘水面对应6英寸鱼身总长
    const double fish_factor { 2.0/0.5 }; // 单位鱼长所需水面面积
    const double inches_per_foot { 12.0 };
    double fish_count {}; // 鱼的数量
    double fish_length {}; // 鱼的平均长度

    std::cout << "Enter the number of fish you want to keep: ";
    std::cin >> fish_count;
    std::cout << "Enter the average fish length in inches: ";
    std::cin >> fish_length;

    fish_length /= inches_per_foot; // 转换为英尺
    std::cout << '\n';
    // 计算所需水面面积
    const double pond_area {fish_count * fish_length * fish_factor};

    // 从面积计算池塘直径
    const double pond_diameter {2.0 * std::sqrt(pond_area / std::numbers::pi)};
    std::cout << fmt::format("Pond diameter required for {} fish is {} feet.\n", fish_count, pond_diameter);
    return 0;
}

使用的编译命令

  • 预编译C++标准库模块:
    g++ -c -fmodules-ts -x c++-system-header iostream numbers cmath -std=c++20
    
  • 编译程序:
    g++ -fmodules-ts -o fish_pond fish_pond.cpp -I /usr/local/include -L /usr/local/lib/ -std=c++20
    

链接错误信息

/usr/bin/ld: /tmp/ccObU8Zj.o: in function `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > fmt::v10::format<double&, double const&>(fmt::v10::basic_format_string<char, fmt::v10::type_identity<double&>::type, fmt::v10::type_identity<double const&>::type>, double&, double const&)':
fish_pond.cpp:(.text._ZN3fmt3v106formatIJRdRKdEEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEENS0_19basic_format_stringIcJDpNS0_13type_identityIT_E4typeEEEEDpOSD_[_ZN3fmt3v106formatIJRdRKdEEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEENS0_19basic_format_stringIcJDpNS0_13type_identityIT_E4typeEEEEDpOSD_]+0xac): undefined reference to `fmt::v10::vformat(fmt::v10::basic_string_view<char>, fmt::v10::basic_format_args<fmt::v10::context>)'
collect2: error: ld returned 1 exit status

已尝试的方法

  • 将#include <fmt/format.h>替换为import <fmt/format.h>并重新预编译模块
  • 添加-lfmt链接选项

当前使用g++版本:Ubuntu 11.4.0-1ubuntu1~22.04(11.4.0)

带-Wall --verbose的编译输出

g++ -Wall --verbose -std=c++20 -fmodules-ts -o fish_pond fish_pond.cpp -lfmt
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 11.4.0-1ubuntu1~22.04' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-11 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-gcn/usr --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04) 
COLLECT_GCC_OPTIONS='-Wall' '-v' '-std=c++20' '-fmodules-ts' '-o' 'fish_pond' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/11/cc1plus -quiet -v -imultiarch x86_64-linux-gnu -D_GNU_SOURCE fish_pond.cpp -quiet -dumpbase fish_pond.cpp -dumpbase-ext .cpp -mtune=generic -march=x86-64 -Wall -std=c++20 -version -fmodules-ts -fasynchronous-unwind-tables -fstack-protector-strong -Wformat-security -fstack-clash-protection -fcf-protection -o /tmp/ccBeX1O1.s
GNU C++20 (Ubuntu 11.4.0-1ubuntu1~22.04) version 11.4.0 (x86_64-linux-gnu)
    compiled by GNU C version 11.4.0, GMP version 6.2.1, MPFR version 4.1.0, MPC version 1.2.1, isl version isl-0.24-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring duplicate directory "/usr/include/x86_64-linux-gnu/c++/11"
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/11/include-fixed"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/11/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/c++/11
 /usr/include/x86_64-linux-gnu/c++/11
 /usr/include/c++/11/backward
 /usr/lib/gcc/x86_64-linux-gnu/11/include
 /usr/local/include
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
GNU C++20 (Ubuntu 11.4.0-1ubuntu1~22.04) version 11.4.0 (x86_64-linux-gnu)
    compiled by GNU C version 11.4.0, GMP version 6.2.1, MPFR version 4.1.0, MPC version 1.2.1, isl version isl-0.24-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: d591828bb4d392ae8b7b160e5bb0b95f
COLLECT_GCC_OPTIONS='-Wall' '-v' '-std=c++20' '-fmodules-ts' '-o' 'fish_pond' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 as -v --64 -o /tmp/ccGVhwRY.o /tmp/ccBeX1O1.s
GNU assembler version 2.38 (x86_64-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.38
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/11/:/usr/lib/gcc/x86_64-linux-gnu/11/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/11/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/11/:/usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/11/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/11/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-Wall' '-v' '-std=c++20' '-fmodules-ts' '-o' 'fish_pond' '-shared-libgcc' '-mtune=generic' '-march=x86-64' '-dumpdir' 'fish_pond.'
 /usr/lib/gcc/x86_64-linux-gnu/11/collect2 -plugin /usr/lib/gcc/x86_64-linux-gnu/11/liblto_plugin.so -plugin-opt=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper -plugin-opt=-fresolution=/tmp/cc42iVWy.res -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu --as-needed -dynamic-linker /lib64/ld-linux-x86-64.so.2 -pie -z now -z relro -o fish_pond /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/Scrt1.o /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/11/crtbeginS.o -L/usr/lib/gcc/x86_64-linux-gnu/11 -L/usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/11/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/11/../../.. /tmp/ccGVhwRY.o -lfmt -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-linux-gnu/11/crtendS.o /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/crtn.o
/usr/bin/ld: /tmp/ccGVhwRY.o: in function `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > fmt::v10::format<double&, double const&>(fmt::v10::basic_format_string<char, fmt::v10::type_identity<double&>::type, fmt::v10::type_identity<double const&>::type>, double&, double const&)':
fish_pond.cpp:(.text._ZN3fmt3v106formatIJRdRKdEEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEENS0_19basic_format_stringIcJDpNS0_13type_identityIT_E4typeEEEEDpOSD_[_ZN3fmt3v106formatIJRdRKdEEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEENS0_19basic_format_stringIcJDpNS0_13type_identityIT_E4typeEEEEDpOSD_]+0xac): undefined reference to `fmt::v10::vformat(fmt::v10::basic_string_view<char>, fmt::v10::basic_format_args<fmt::v10::context>)'
collect2: error: ld returned 1 exit status

解决方案

1. 修正编译命令的库路径与链接顺序

错误显示引用的是fmt::v10的符号,说明安装的是fmt 10.x版本。编译时需明确指定库路径并确保链接选项位置正确:

g++ -fmodules-ts -o fish_pond fish_pond.cpp -I/usr/local/include -L/usr/local/lib -std=c++20 -lfmt

2. 放弃将fmt作为C++模块导入

GCC 11对第三方库的C++模块支持不完善,继续使用#include <fmt/format.h>,且不要尝试将fmt作为系统模块预编译。

3. 确认fmt库的安装完整性

运行以下命令检查库文件是否存在:

ls /usr/local/lib/libfmt*

若未找到libfmt.so或libfmt.a,重新编译安装fmt:

git clone https://github.com/fmtlib/fmt.git
cd fmt
mkdir build && cd build
cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local
make -j$(nproc)
sudo make install

4. 尝试静态链接

若动态链接仍有问题,直接指定静态库文件进行链接:

g++ -fmodules-ts -o fish_pond fish_pond.cpp -I/usr/local/include -L/usr/local/lib -std=c++20 /usr/local/lib/libfmt.a

内容的提问来源于stack exchange,提问作者zizu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 21:35:53