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

相关推荐
小涛不学习3 小时前
手写线程池(从0实现 ThreadPoolExecutor 核心思想)
windows
twc8294 小时前
大模型生成 QA Pairs 提升 RAG 应用测试效率的实践
服务器·数据库·人工智能·windows·rag·大模型测试
wenlonglanying5 小时前
Windows安装Rust环境(详细教程)
开发语言·windows·rust
polaris06306 小时前
Windows操作系统部署Tomcat详细讲解
java·windows·tomcat
mldlds6 小时前
windows手动配置IP地址与DNS服务器以及netsh端口转发
服务器·windows·tcp/ip
Rust研习社7 小时前
Rust + WebAssembly 新手完全入门指南
rust
取个名字太难了a7 小时前
DebugActiveProcess 调试流程分析(一)
windows
Java.熵减码农7 小时前
火绒安全软件误杀explorer.exe导致黑屏解决方法
windows
love530love7 小时前
不用聊天软件 OpenClaw 手机浏览器远程访问控制:Tailscale 配置、设备配对与常见问题全解
人工智能·windows·python·智能手机·tailscale·openclaw·远程访问控制
夏末蝉未鸣018 小时前
Windows环境下载并安装milvus
windows·milvus