4

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

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

rodrigocfd
  • 6,450
  • 6
  • 34
  • 68

1 Answers1

7

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.

Ruben
  • 95
  • 1
  • 6
Hadus
  • 1,551
  • 11
  • 22
  • Do you have an example of how such option can be added to Cargo.toml? – rodrigocfd Feb 25 '21 at 13:14
  • I am not sure how to do it in the `Cargo.toml` but you can set the environmental variable for it `export CARGO_CFG_TARGET_FEATURE=crt-static` or at least that is what the RFC says. – Hadus Feb 25 '21 at 13:30
  • 3
    or another example from the RFC: `RUSTFLAGS='-C target-feature=+crt-static' cargo build --target x86_64-pc-windows-msvc` – Hadus Feb 25 '21 at 13:31
  • 1
    I edited the answer so you can specify the rustflags in config.toml instead of environmental variables. – Hadus Feb 25 '21 at 13:37
  • I'm getting `error: unknown codegen option: target-feature`, no idea what's wrong. – rodrigocfd Feb 25 '21 at 14:03
  • 1
    I ended up passing the linker flag in the command line: `RUSTFLAGS='-C target-feature=+crt-static' cargo build --target x86_64-pc-windows-msvc`. Thank you. – rodrigocfd May 27 '21 at 01:11