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

Windows10下如何为cl.exe传递fmt库以完成链接编译

问题描述

我在hello.cpp中有如下代码片段:

#include "fmt/core.h"

int main()
{
    fmt::println("Hello from fnt!");
}

我尝试编译这段代码,知道需要添加额外的链接选项才能成功编译。当前使用的编译命令为:

cl /EHsc /I C:\dev\vcpkg\packages\fmt_x64-windows\include hello.cpp

编译输出如下:

Microsoft (R) C/C++ Optimizing Compiler Version 19.38.33126.1 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

hello.cpp
Microsoft (R) Incremental Linker Version 14.38.33126.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:hello.exe
hello.obj
hello.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) void __cdecl fmt::v10::detail::vprint_mojibake(struct _iobuf *,class fmt::v10::basic_string_view<char>,class fmt::v10::basic_format_args<class fmt::v10::basic_format_context<class fmt::v10::appender,char> >)" (__imp_?vprint_mojibake@detail@v10@fmt@@YAXPAU_iobuf@@V?$basic_string_view@D@23@V?$basic_format_args@V?$basic_format_context@Vappender@v10@fmt@@D@v10@fmt@@@23@@Z),该符号在函数 "void __cdecl fmt::v10::print<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(struct _iobuf *,class fmt::v10::basic_format_string<char,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &&)" (??$print@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@v10@fmt@@YAXPAU_iobuf@@V?$basic_format_string@DV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@01@$$QAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) 中被引用
hello.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl fmt::v10::vformat(class fmt::v10::basic_string_view<char>,class fmt::v10::basic_format_args<class fmt::v10::basic_format_context<class fmt::v10::appender,char> >)" (__imp_?vformat@v10@fmt@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$basic_string_view@D@12@V?$basic_format_args@V?$basic_format_context@Vappender@v10@fmt@@D@v10@fmt@@@12@@Z),该符号在函数 "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl fmt::v10::format<>(class fmt::v10::basic_format_string<char>)" (??$format@$$V@v10@fmt@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$basic_format_string@D$$V@01@@Z) 中被引用
hello.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) void __cdecl fmt::v10::vprint(struct _iobuf *,class fmt::v10::basic_string_view<char>,class fmt::v10::basic_format_args<class fmt::v10::basic_format_context<class fmt::v10::appender,char> >)" (__imp_?vprint@v10@fmt@@YAXPAU_iobuf@@V?$basic_string_view@D@12@V?$basic_format_args@V?$basic_format_context@Vappender@v10@fmt@@D@v10@fmt@@@12@@Z),该符号在函数 "void __cdecl fmt::v10::print<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(struct _iobuf *,class fmt::v10::basic_format_string<char,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &&)" (??$print@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@v10@fmt@@YAXPAU_iobuf@@V?$basic_format_string@DV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@01@$$QAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) 中被引用
hello.exe : fatal error LNK1120: 3 个无法解析的外部命令

我已通过vcpkg安装fmt库并完成集成配置,请问该如何解决上述链接错误?

解决方法

这些错误是因为编译命令仅指定了头文件路径,未告诉链接器要关联fmt的库文件。针对vcpkg安装的fmt,有两种可行方案:

方案一:直接在命令中添加库文件

找到vcpkg安装的fmt库文件路径(通常在C:\dev\vcpkg\packages\fmt_x64-windows\lib),将库文件加入编译命令:

cl /EHsc /I C:\dev\vcpkg\packages\fmt_x64-windows\include hello.cpp C:\dev\vcpkg\packages\fmt_x64-windows\lib\fmt.lib

如果编译Debug版本,需替换为fmtd.lib:

cl /EHsc /I C:\dev\vcpkg\packages\fmt_x64-windows\include hello.cpp C:\dev\vcpkg\packages\fmt_x64-windows\debug\lib\fmtd.lib

方案二:通过链接选项指定库目录与库名

利用/link参数指定库文件所在目录和库名,简化命令:

cl /EHsc /I C:\dev\vcpkg\packages\fmt_x64-windows\include hello.cpp /link /LIBPATH:C:\dev\vcpkg\packages\fmt_x64-windows\lib fmt.lib

Debug版本对应命令:

cl /EHsc /I C:\dev\vcpkg\packages\fmt_x64-windows\include hello.cpp /link /LIBPATH:C:\dev\vcpkg\packages\fmt_x64-windows\debug\lib fmtd.lib

额外注意事项

  • 架构匹配:你安装的是fmt_x64-windows,但当前编译器显示是for x86,存在架构不匹配问题。需切换到x64编译环境(比如打开VS的x64 Native Tools Command Prompt)再执行编译。
  • 确认vcpkg安装:若架构不匹配,可重新执行vcpkg install fmt:x64-windows确保安装对应版本的库。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 23:07:41