如何移除Rust程序对ucrtbase.dll的依赖?
解决Rust程序在Windows Server 2008 R2上依赖ucrtbase.dll的问题
直接用-Ctarget-feature=+crt-static没效果,大概率是目标平台配置或工具链的问题,试试下面的方法:
指定目标三元组编译
编译时明确指定MSVC目标平台,同时带上静态CRT参数,以64位程序为例:cargo build --target x86_64-pc-windows-msvc --release -C target-feature=+crt-static32位程序替换目标三元组为
i686-pc-windows-msvc。通过Cargo配置持久化设置
不想每次编译都输入长命令的话,在项目根目录创建.cargo/config.toml,添加以下内容:[target.x86_64-pc-windows-msvc] rustflags = ["-C", "target-feature=+crt-static"]32位程序对应修改目标三元组即可。
切换到MSVC工具链
如果你当前用的是GNU工具链(比如x86_64-pc-windows-gnu),crt-static参数不会生效——因为GNU工具链依赖mingw运行时。需要切换到MSVC工具链:- 安装Visual Studio Build Tools,勾选「MSVC v142 - VS 2019 C++ x64/x86生成工具」(选兼容Windows Server 2008 R2的版本)
- 安装Rust的MSVC目标:
rustup target add x86_64-pc-windows-msvc
验证静态链接结果
不要用Linux的ldd工具,在Windows上用dumpbin验证依赖:dumpbin /dependents target/x86_64-pc-windows-msvc/release/app.exe输出中如果没有
ucrtbase.dll,说明静态链接成功,程序可以在Windows Server 2008 R2上运行。
内容的提问来源于stack exchange,提问作者Zezombye
相关产品推荐
相关产品推荐

