Skip to content

Use /MDd flag to compile in debug mode if cargo is in debug mode#167

Open
itsjunetime wants to merge 1 commit into
purpleprotocol:masterfrom
itsjunetime:fix_mimalloc_windows_linker
Open

Use /MDd flag to compile in debug mode if cargo is in debug mode#167
itsjunetime wants to merge 1 commit into
purpleprotocol:masterfrom
itsjunetime:fix_mimalloc_windows_linker

Conversation

@itsjunetime

Copy link
Copy Markdown

If mimalloc_rust is used in conjunction with another msvc library, it needs to set the correct run-time library version (as specified by this microsoft doc). Previously, mimalloc_rust was always building mimalloc_rust in DynamicRelease mode, which would conflict with other libraries building against msvc who were correctly respecting the debug level (and building in DynamicDebug mode).

This issue can be seen in the CI runs of this PR, where mupdf-rs was correctly building in debug mode when cargo was building in debug mode, but the mimalloc DLL was building in release mode still. The linker refused to link the libraries together because of the differing runtime library mode.

As you can see from the later commits on the aforementioned PR, this change fixes the issue. For some reason, using build.flag_if_supported didn't do the trick - I guess the build system isn't aware that /MDd is a valid flag.

@nathaniel-daniel

nathaniel-daniel commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

I think this is an issue with the mupdf/msbuild crate? Rust doesn't support the non-debug runtime.

@itsjunetime

Copy link
Copy Markdown
Author

Hmm, I'm not sure that I agree. Looking at this comment and comparing it to the linking that mupdf requests, I think that mupdf is handling this better. They specifically request to link with the debug runtime when it's in debug mode, and the release runtime when in release mode. That just conflicts with rust's default behavior, which makes it work weirdly when combined with libraries (like mimalloc) which use rust's default behavior.

So I think the best course of action is actually to add this /MDd flag when in debug mode for mimalloc-sys and also add the special link arguments that the aforementioned comment and mupdf utilize. This could theoretically cause linker errors when mimalloc is then combined with other C libraries that don't use this special linking, but I think that's probably fine since it means that they weren't doing their linking correctly anyways?

Does everything I'm saying here line up with your understanding? I feel a bit out of my depth since I don't interact with windows much.

@nathaniel-daniel

Copy link
Copy Markdown
Contributor

The main issue is that you can't really coordinate this change with everything else. I'd argue this belongs in cc. cc has tried to fix this in the past, but that ended up creating a lot of breakage, so they reverted. It looks like cc unconditionally links the non-debug runtime currently.

Also, an msrv of 1.79 would probably require a few crates to bump msrv for this.

This is kind of an annoying/complicated issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants