Linking against debug MS VC++ Runtime

On Windows even in Debug mode Rust still links against the Release runtime.
This is annoying, since the C++ library Rust interacts with is linked against Debug runtime, which causes conflicts.

I found this issue: rustc always links against non-debug Windows runtime · Issue #39016 · rust-lang/rust · GitHub

It says that I can use this code to use Debug runtime instead:

// Don't link the default CRT
println!("cargo::rustc-link-arg-bins=/nodefaultlib:msvcrt");
// Link the debug CRT instead
println!("cargo::rustc-link-arg-bins=/defaultlib:msvcrtd");

The problem with this code, however, is that I cannot run this as a part of a Rust library build.rs script, as I get this error:

error: invalid instruction `cargo::rustc-link-arg-bins` from build script

I tried setting cargo::rustc-link-lib instead, but this failed also:

// Don't link the default CRT
println!("cargo::rustc-link-lib=/nodefaultlib:msvcrt");
// Link the debug CRT instead
println!("cargo::rustc-link-lib=/defaultlib:msvcrtd");

...

error: renaming of the library `/nodefaultlib` was specified, however this crate contains no `#[link(...)]` attributes referencing this library
error: renaming of the library `/defaultlib` was specified, however this crate contains no `#[link(...)]` attributes referencing this library

Can anything be done now?

Okay, there is just cargo::rustc-link-arg, sadly it doesn't seem like arguments to the linker are passed transitevely.

the msvc linker support embedded linker directives in a special section named ".drectve", and a crate link-args implements this feature in rust, maybe you can try it.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.