How to generate statically linked executables?

前端 未结 2 793
长发绾君心
长发绾君心 2020-12-13 06:11

I am trying to create a static executable with Rust. I am not trying to statically link a particular library, I am trying to create a executable which does not

相关标签:
2条回答
  • 2020-12-13 06:55

    Since Rust 1.19, you can statically link the C runtime (CRT) to avoid this very common situation on Windows:

    The program can't start because VCRUNTIME140.dll is missing from your computer. Try reinstalling the program to fix this problem.

    Add this to your .cargo/config file, using the appropriate target triple for your platform:

    [target.x86_64-pc-windows-msvc]
    rustflags = ["-C", "target-feature=+crt-static"]
    

    An alternative to editing .cargo/config is to pass -C target-feature=+crt-static to rustc by hand.

    See also:

    • RFC

    • Pull Request

    • Cargo config documentation

    0 讨论(0)
  • 2020-12-13 06:55

    Rust statically links everything but glibc (and libgcc, iirc) by default.

    If you want to get a 100% statically linked binary, you can use MUSL with 1.1. https://github.com/rust-lang/rust/pull/24777 is the initial support, we hope to make it much easier to use in the future.

    0 讨论(0)
提交回复
热议问题