【问题标题】:Does Rust/Cargo link statically to MSVCRT?Rust/Cargo 是否静态链接到 MSVCRT?
【发布时间】:2021-02-25 11:33:16
【问题描述】:

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

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

【问题讨论】:

    标签: rust rust-cargo compiler-options


    【解决方案1】:

    我认为你可以指定它。这是启用它的 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 的示例吗?
    • 我不确定如何在 Cargo.toml 中执行此操作,但您可以为它设置环境变量 export CARGO_CFG_TARGET_FEATURE=crt-static 或至少 RFC 是这么说的。
    • 或 RFC 中的另一个示例:RUSTFLAGS='-C target-feature=+crt-static' cargo build --target x86_64-pc-windows-msvc
    • 我编辑了答案,因此您可以在 config.toml 中指定 rustflags 而不是环境变量。
    • 我收到了error: unknown codegen option: target-feature,不知道怎么了。
    【解决方案2】:

    您可以在.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 的链接方式。
    猜你喜欢
    • 2011-04-25
    • 2019-08-27
    • 2020-12-16
    • 2020-01-15
    • 2013-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    相关资源
    最近更新 更多