【发布时间】:2021-02-25 11:33:16
【问题描述】:
在 Windows 中,编译 C++ 时,我可以指定 /MT compiler option 以使用运行时库的静态版本,即。 不动态链接到 MSVCRT。
Rust/Cargo 在这方面的表现如何,因为没有这样的选项?它是静态链接还是动态链接?
【问题讨论】:
标签: rust rust-cargo compiler-options
在 Windows 中,编译 C++ 时,我可以指定 /MT compiler option 以使用运行时库的静态版本,即。 不动态链接到 MSVCRT。
Rust/Cargo 在这方面的表现如何,因为没有这样的选项?它是静态链接还是动态链接?
【问题讨论】:
标签: rust rust-cargo compiler-options
我认为你可以指定它。这是启用它的 RFC: https://github.com/rust-lang/rfcs/blob/master/text/1721-crt-static.md
在rustc book中有一个页面提到它。
这也支持功能 +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"]
...
虽然我还没有尝试过,但我想它应该可以工作。
【讨论】:
Cargo.toml 中执行此操作,但您可以为它设置环境变量 export CARGO_CFG_TARGET_FEATURE=crt-static 或至少 RFC 是这么说的。
RUSTFLAGS='-C target-feature=+crt-static' cargo build --target x86_64-pc-windows-msvc
error: unknown codegen option: target-feature,不知道怎么了。
您可以在.cargo 文件中指定您的目标:https://stackoverflow.com/a/49453658/1470802
[build]
target = "x86_64-pc-windows-msvc"
也可以通过命令行设置默认target。
在Cargo.toml 中需要指定库类型(在您的情况下为"staticlib")。
[lib]
name = "libname"
crate-type = ["dylib", "staticlib"]
【讨论】:
msvc 的链接方式。