Rust Windows下编译 静态链接VCRuntime140.dll

Rust 编译出来的exe默认动态链接VC运行库,分发电脑上需要安装有Microsoft Visual C++ Redistributable for Visual Studio 2015运行库。

编译时能静态链接进去,就省去客户端未安装运行库的问题。方法如下:

只需在当前根目录下新建.cargo\config.toml,写入以下配置

目录结构

bash 复制代码
├── .cargo
│   └── config.toml
├── Cargo.lock
├── Cargo.toml
└── src
    └── main.rs

.cargo\config.toml

toml 复制代码
[target.'cfg(all(windows, target_env = "msvc"))']
rustflags = ["-C", "target-feature=+crt-static"]

测试代码main.rs

rust 复制代码
use windows::{core::h, Win32::UI::WindowsAndMessaging::{MessageBoxW, MB_OK}};

fn main() {
    unsafe{
        MessageBoxW(None, h!("Demo"), h!("自带vcruntime140.dll"), MB_OK);
    }
}

Cargo.toml

toml 复制代码
[package]
name = "rust_vc_runtime"
version = "0.1.0"
edition = "2021"

[dependencies.windows]
version = "0.58.0"
features = [
    "Win32_UI_WindowsAndMessaging",
    "Win32_UI_Shell"
]

添加.cargo\config.toml和未添加.cargo\config.toml结果

相关推荐
默默无名的大学生28 分钟前
数据结构—顺序表
数据结构·windows
湖南馒头1 小时前
【Win11 启动项BCD文件修复教程】
windows·系统恢复·win1
帅得不敢出门6 小时前
macOS苹果电脑运行向日葵远程控制软件闪退
windows·macos·远程控制·向日葵
编码浪子10 小时前
趣味学RUST基础篇(函数式编程闭包)
开发语言·算法·rust
索迪迈科技11 小时前
记一次 .NET 某中医药附属医院门诊系统 崩溃分析
windows·c#·.net·windbg
十八旬11 小时前
苍穹外卖项目实战(day7-2)-购物车操作功能完善-记录实战教程、问题的解决方法以及完整代码
java·开发语言·windows·spring boot·mysql
光影少年13 小时前
css优化都有哪些优化方案
前端·css·rust
focksorCr14 小时前
编译缓存工具 sccache 效果对比
c++·缓存·rust
Clownseven16 小时前
阿里云ECS安装Windows Server 2022教程 | 纯文字分步指南
windows·阿里云·云计算
T0uken16 小时前
【C++】LLVM-mingw + VSCode:Windows 开发攻略
c++·windows·vscode