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结果

相关推荐
SomeOtherTime44 分钟前
Rust生命周期介绍(AI回答)
开发语言·后端·rust
SomeOtherTime14 小时前
Rust所有权(AI回答)
开发语言·后端·rust
Zucker n1 天前
文献发现、课题组织与证据可追溯的论文阅读。
论文阅读·windows·ubuntu
SomeOtherTime1 天前
Rust 面向对象介绍(AI回答)
开发语言·python·rust
flyingonpine2 天前
PowerShell脚本数字签名实战:自签名证书、代码签名与信任链验证
windows·信任链
goehou2 天前
让 AI 替我操作这台电脑,先把手能伸多远定下来
windows·自动化·权限管理·ai agent
西风测评2 天前
安全的自动更新怎么做:minisign 签名、先验签后解析与防回滚
rust·ssh
公子小六2 天前
基于.NET的Windows窗体编程之WinForms数据持久化(1)
windows·microsoft·c#·.net·winforms
Delightrs2 天前
Rust LeetCode238 算法题解
开发语言·算法·rust
奶油话梅糖2 天前
快过 IDM 5 倍、开源免费:4,200 Star 的 Rust 下载器用 7 组对照实验“单挑“行业老大
rust·开源·idm