llvm-mingw
llvm-mingw copied to clipboard
Linking with the static UCRT runtime (/MT)
Is it possible with this project to link to the UCRT static runtime, so that programs built with MSVC with the /MT flag can link with libraries built with this project?
I am able to link libraries built with cross compilers from here to programs building with MSVC on windows but only when the /MD flag is used, if I try to use the /MT flag to use the static runtime link.exe
fails, complaining about mismatched runtime types.
Am I missing a magic clang flag to make this work or is it just not possible?
mingw-w64 has no support for linking against static UCRT, likely due to licensing reasons.
The answer is twofold:
-
mingw-w64 doesn't, in general, support linking against a static CRT. The main premise of mingw is having a freely redistributable SDK, with headers reimplemented from scratch, and the interface of the dynamically linked CRT (either msvcrt.dll or UCRT) described so that programs can be linked against it. The static CRT (as shipped part of MSVC) isn't freely redistributable under the same terms.
-
In general, you cannot mix object files or static libraries built with mingw-w64 with ones from MSVC. Interoperability is supported across DLL boundaries. (In general, interop is only supported as long as you don't share CRT resources across the DLL boundary. But if both are linked against the same shared UCRT, DLL boundaries where you share CRT resources work too.) The interface between what's defined in headers (e.g. as inline functions) and what's linked in via small stubs in the import library isn't compatible between MSVC and mingw-w64 (and even within mingw-w64, it is tweaked occasionally). If mixing object files or static libraries between MSVC and mingw-w64 works for you currently, with a shared linked UCRT, I would consider that a lucky coincidence. It most definitely doesn't work for C++, and I can come up with a handful of cases where it would break for regular C code as well.
If you happen to be in a lucky usecase where mixing object files or static libraries happen to work, but if it fails when linking with /MT
, it might be possible to stretch that case a little bit and inject the right directives to appease the link.exe
check - if you'd share the error message you get, but it's far in the territory of unsupported hacks which probably won't be upstreamed.