Rust 编译出来的exe默认动态链接VC运行库,分发电脑上需要安装有Microsoft Visual C++ Redistributable for Visual Studio 2015运行库。
编译时能静态链接进去,就省去客户端未安装运行库的问题。方法如下:
只需在当前根目录下新建.cargo\config.toml,写入以下配置
目录结构
├── .cargo
│ └── config.toml
├── Cargo.lock
├── Cargo.toml
└── src└── main.rs
.cargo\config.toml
[target.'cfg(all(windows, target_env = "msvc"))']
rustflags = ["-C", "target-feature=+crt-static"]
测试代码
main.rs
use windows::{core::h, Win32::UI::WindowsAndMessaging::{MessageBoxW, MB_OK}};fn main() {unsafe{MessageBoxW(None, h!("Demo"), h!("自带vcruntime140.dll"), MB_OK);}
}
Cargo.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
结果