Rust/Cargo 是否静态链接到 MSVCRT?

问题描述

在 Windows 中,在编译 C++ 时,我可以指定 /MT compiler option 来使用运行时库的静态版本,即。 动态链接到 MSVCRT。

Rust/Cargo 在这方面表现如何,因为没有这样的选择?它是静态链接还是动态链接

解决方法

我认为你可以指定它。这是启用它的 RFC: https://github.com/rust-lang/rfcs/blob/master/text/1721-crt-static.md

有一个页面在 rustc book 中提到了它。

这也支持功能 +crt-static 和 -crt-static 来控制静态 C 运行时链接。

创建一个名为 .config/config.toml 的文件,您可以在其中指定 rustflags

https://doc.rust-lang.org/cargo/reference/config.html

内部config.toml

...

[build]
rustflags = ["-C target-feature=+crt-static"]
...

我还没有尝试过这个,但我想它应该可以工作。

,

您可以在 .cargo 文件中指定目标:https://stackoverflow.com/a/49453658/1470802

[build]
target = "x86_64-pc-windows-msvc"

也可以通过命令行设置默认 target

Cargo.toml 中必须指定 lib 类型(在您的情况下为 "staticlib")。

[lib]
name = "libname"
crate-type = ["dylib","staticlib"]