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

相关推荐
vfvfb6 小时前
bat批量去掉本文件夹中的文件扩展名
服务器·windows·批处理·删除扩展名·bat技巧
维维酱11 小时前
Rust - 互斥锁
rust
维维酱11 小时前
Rust - 共享状态的并发
rust
我命由我1234512 小时前
VSCode - VSCode 放大与缩小代码
前端·ide·windows·vscode·前端框架·编辑器·软件工具
PT_silver12 小时前
tryhackme——Abusing Windows Internals(进程注入)
windows·microsoft
ArcX13 小时前
从 JS 到 Rust 的旅程
前端·javascript·rust
Humbunklung13 小时前
Rust Floem UI 框架使用简介
开发语言·ui·rust
爱炸薯条的小朋友13 小时前
C#由于获取WPF窗口名称造成的异常报错问题
windows·c#·wpf
Lw老王要学习14 小时前
VScode 使用 git 提交数据到指定库的完整指南
windows·git·vscode
寻月隐君18 小时前
深入解析 Rust 的面向对象编程:特性、实现与设计模式
后端·rust·github