rustrust-cargocompiler-options

Does Rust/Cargo link statically to MSVCRT?


In Windows, when compiling C++, I can specify /MT compiler option to use a static version of the runtime library, ie. to not link dynamically to MSVCRT.

How does Rust/Cargo behave in this regard, since there is no such option? Does it link static or dynamically?


Solution

  • You can specify it I think. Here is the RFC that enabled it: https://github.com/rust-lang/rfcs/blob/master/text/1721-crt-static.md

    There is a page that mentions it in the rustc book.

    This also supports the feature +crt-static and -crt-static to control static C runtime linkage.

    Make a file called .cargo/config.toml and inside it you can specify the rustflags

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

    Inside config.toml

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

    I have not tried this yet though but I imagine it should work.