Static CRT for specific crate

I have a project which I'd like to produce a binary with a statically linked CRT. The Internet(tm) states that one should edit ~/.cargo/config to include:

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

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

But this is a global change; I want to hardcode it in this one create.

I get why it's designed the way it is -- it should be up to the person building the crate to decide what CRT to link into the binary, but I have this one specific case where it makes sense to force static CRT within the crate itself.

Hm. The more I think about it, the more I realize why it's a bad idea to allow crates themselves to force a static CRT. Still wondering if there's a way though. :teddy_bear:

You should be able to put this in .cargo/config inside the project.

1 Like

Wait, what?!

I've been reading .cargo/config to mean $HOME/.cargo/config all this time. :man_facepalming:

Yes, that's the global config, but the project-local one, AFAIK, can override the global.

You may also be interested in reading the page on cargo's config file from The Cargo Book.

It explains how cargo's configuration system is resolved and what you can do in a .cargo/config.

1 Like