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

相关推荐
云服务器租用费用8 小时前
2026年腾讯云OpenClaw(Clawdbot)+Skills云上部署及Windows本地集成轻松入门
运维·服务器·数据库·windows·云计算·腾讯云
烛之武8 小时前
Python速通笔记
windows·python
牛奶咖啡139 小时前
windows系统中安装配置命令行工具——psping
windows·psping·psping与ping对比·下载安装配置psping·psping的核心功能与用法·psping参数
2601_961194029 小时前
2026六级词汇PDF下载|大学英语六级单词表+音频PDF
windows·git·eclipse·pdf·github
Yuyubow9 小时前
gpui step by step 3. 消息传递 EventEmitter
rust
不爱学英文的码字机器11 小时前
[鸿蒙PC命令行移植适配]移植rust三方库tokei到鸿蒙PC的完整实践
华为·rust·harmonyos
EterNity_TiMe_11 小时前
[鸿蒙PC命令行移植适配]移植rust三方库ouch到鸿蒙PC的完整实践
华为·rust·harmonyos
ID_1800790547311 小时前
小红书笔记评论 API 接口深度解析(带全套 JSON 示例・技术实战版)
java·开发语言·windows
EterNity_TiMe_12 小时前
[鸿蒙PC命令行移植适配]移植rust三方库broot到鸿蒙PC的完整实践
华为·rust·harmonyos
xyz59912 小时前
Windows下Codex安装详细配置使用指南
windows