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

相关推荐
gameatp1 小时前
从 Windows 到 Linux 服务器的全自动部署教程(免密登录 + 压缩 + 上传 + 启动)
linux·服务器·windows
穷人小水滴1 小时前
在 windows 运行 flatpak 应用 (WSL)
linux·windows·ubuntu
北十南1 天前
SODA自然美颜相机(甜盐相机国际版) v9.3.0
android·windows·数码相机
nbsaas-boot1 天前
用 FreeMarker 动态构造 SQL 实现数据透视分析
数据库·windows·sql·freemarker·数据报表
weixin_446260851 天前
windows下hashcat使用gpu破解execl打开密码
windows
Evaporator Core1 天前
Windows批处理脚本自动合并当前目录下由You-get下载的未合并的音视频文件
windows·音视频
十五年专注C++开发2 天前
CMake进阶: externalproject_add用于在构建阶段下载、配置、构建和安装外部项目
linux·c++·windows·cmake·自动化构建
guidovans2 天前
基于tkinter开发电脑工具集(源码在底部)
linux·windows·python·gui·tkinter
我是前端小学生2 天前
一文梳理Rust语言中的可变结构体实例
rust
Source.Liu2 天前
【unitrix数间混合计算】2.21 二进制整数加法计算(bin_add.rs)
rust