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

Windows下使用Boost.Serialization遭遇LNK1104链接错误

Boost.Serialization 链接错误排查(Windows 10 + VC142 + Boost 1.80.0)

问题描述

已编译Boost 1.80.0并正常使用Container等模块,但引入Boost.Serialization后触发链接错误:

LINK : fatal error LNK1104: cannot open file 'libboost_serialization-vc142-mt-s-x64-1_80.lib'

查看Boost库存放目录,仅存在相似命名的库文件,无带-mt-s-x64后缀的版本。

编译命令详情

预编译命令

@call cl.exe ^
/c ^
/Ycstdafx.hpp ^
src/stdafx.cpp ^
/Yustdafx.hpp ^
/Fo:obj_debug\ ^
/Fe:dist\swan_debug.exe ^
/Fd:dist\vc140_debug.pdb ^
/I"C:/code/glfw" ^
/I"C:/code/boost_1_80_0" ^
/std:c++20 ^
/W4 ^
/EHsc ^
/MP ^
/MT ^
/D_CRT_SECURE_NO_WARNINGS ^
/Zi ^
/link ^
/DEBUG:FULL ^
/NODEFAULTLIB:MSVCRTD ^
/NODEFAULTLIB:LIBCMT ^
/LIBPATH:"C:/code/glfw" ^
/LIBPATH:"C:/code/boost_1_80_0/stage/lib" ^
glfw3.lib ^
opengl32.lib ^
gdi32.lib ^
shell32.lib ^
kernel32.lib ^
msvcrt.lib ^
ole32.lib ^
shlwapi.lib ^
Pathcch.lib

主编译命令

@call cl.exe ^
src/all_in_one.cpp ^
obj_debug/imgui_demo.obj ^
obj_debug/imgui_draw.obj ^
obj_debug/imgui_impl_glfw.obj ^
obj_debug/imgui_impl_opengl3.obj ^
obj_debug/imgui_tables.obj ^
obj_debug/imgui_widgets.obj ^
obj_debug/imgui.obj ^
obj_debug/stb_image.obj ^
obj_debug/stdafx.obj ^
/Fo:obj_debug\ ^
/Fe:dist\swan_debug.exe ^
/Fd:dist\vc140_debug.pdb ^
/Yustdafx.hpp ^
/I"C:/code/glfw" ^
/I"C:/code/boost_1_80_0" ^
/std:c++20 ^
/nologo ^
/W4 ^
/EHsc ^
/MP ^
/MT ^
/D_CRT_SECURE_NO_WARNINGS ^
/Zi ^
/link ^
/NATVIS:swan.natvis ^
/DEBUG:FULL ^
/NODEFAULTLIB:MSVCRTD ^
/NODEFAULTLIB:LIBCMT ^
/LIBPATH:"C:/code/glfw" ^
/LIBPATH:"C:/code/boost_1_80_0/stage/lib" ^
glfw3.lib ^
opengl32.lib ^
gdi32.lib ^
shell32.lib ^
kernel32.lib ^
msvcrt.lib ^
ole32.lib ^
shlwapi.lib ^
Pathcch.lib ^
Dbghelp.lib

编译器输出

all_in_one.cpp
Microsoft (R) Incremental Linker Version 14.29.30148.0    
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:dist\swan_debug.exe
/debug
/NATVIS:swan.natvis
/DEBUG:FULL
/NODEFAULTLIB:MSVCRTD
/NODEFAULTLIB:LIBCMT
/LIBPATH:C:/code/glfw
/LIBPATH:C:/code/boost_1_80_0/stage/lib
glfw3.lib
opengl32.lib
gdi32.lib
shell32.lib
kernel32.lib
msvcrt.lib
ole32.lib
shlwapi.lib
Pathcch.lib
Dbghelp.lib
obj_debug\all_in_one.obj
obj_debug/imgui_demo.obj
obj_debug/imgui_draw.obj
obj_debug/imgui_impl_glfw.obj
obj_debug/imgui_impl_opengl3.obj
obj_debug/imgui_tables.obj 
obj_debug/imgui_widgets.obj
obj_debug/imgui.obj
obj_debug/stb_image.obj
obj_debug/stdafx.obj
LINK : fatal error LNK1104: cannot open file 'libboost_serialization-vc142-mt-s-x64-1_80.lib'

原因分析

链接器寻找的库文件名后缀对应Boost的编译参数规则:

  • -mt:多线程编译模式
  • -s:静态链接C/C++运行时库
  • x64:64位架构

你的项目编译参数中使用了/MT(静态链接多线程运行时库),Boost的自动链接机制会根据这个参数自动推导需要匹配的库版本。但你之前编译Boost时,没有生成对应/MT模式的静态库,导致链接器找不到匹配文件。

解决步骤

  1. 重新编译Boost生成对应版本库
    打开VC142对应的开发者命令提示符,进入Boost根目录,执行以下命令:

    bootstrap.bat
    b2 toolset=msvc-14.2 address-model=64 link=static runtime-link=static threading=multi --with-serialization stage
    

    参数说明:

    • toolset=msvc-14.2:指定VC142编译器
    • address-model=64:生成64位库
    • link=static:生成静态Boost库
    • runtime-link=static:对应项目/MT参数,静态链接运行时
    • threading=multi:多线程模式
    • --with-serialization:仅编译Serialization模块(去掉此参数可编译所有模块)
  2. 验证库文件生成
    编译完成后,检查boost_1_80_0/stage/lib目录,确认存在libboost_serialization-vc142-mt-s-x64-1_80.lib文件。

  3. 重新编译项目
    再次执行你的编译命令,链接错误即可解决。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 14:35:02