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

相关推荐
jack186898 小时前
百度 AI 搜索实战:从精准获客到智能决策的应用全景
windows
道满科技9 小时前
Function Calling 参数总出错?只修参数就行
linux·windows·python
GHN198788810 小时前
开源小工具按一下空格,什么都看见了
windows
geovindu12 小时前
rust:Memento Pattern(续)
开发语言·后端·设计模式·rust·行为模式·备忘录模
围炉聊科技12 小时前
Windows-MCP 拆解与实测:AI 操作 Windows 这件事——智能体基建系列
windows·llm·dom
面包狗AI4S12 小时前
ChatGPT/Codex Windows:本地桌面端应用离线包安装与免安装启动方法(无需管理员权限)
人工智能·windows·ai·chatgpt
自养Agent13 小时前
MCP Server Boundary Audit Report
linux·服务器·人工智能·windows·大模型·llm
Kapaseker16 小时前
同样 4 行代码,C++ 编译通过,Rust 为什么直接拒绝?
rust
2301_7867566016 小时前
中科院分区表停发后,期刊评价的技术逻辑:从期刊超越指数到多指标选刊方案
java·数据库·windows
是我知白哒18 小时前
下载的图片Windows上无法设置为屏保如何解决
windows