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

Rod*_*igo 4 compiler-options rust rust-cargo

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

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

Had*_*dus 7

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

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

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

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

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

里面config.toml

...

[build]
rustflags = ["-C", "target-feature=+crt-static"]
...
Run Code Online (Sandbox Code Playgroud)

我还没有尝试过,但我想它应该有效。

  • 或 RFC 中的另一个示例:`RUSTFLAGS='-C target-feature=+crt-static' Cargo build --target x86_64-pc-windows-msvc` (3认同)