Link against debug version of the UCRT?
Although this is more of a Mingw-w64 question, I thought it might be better to ask it here. I was wondering if it's possible to link against debug version of the UCRT using this toolchain? Or is this not possible due to licensing concerns (since the debug version of the UCRT is not redistributable)?
Note that it's not my intention to distribute a debug build, it's just a convenient way to know the exact reason why a program fails. Currently, when one of the following test programs is cross-compiled and executed, it will fail with a backtrace to ucrtbase!_invalid_parameter_noinfo, which does not appear to contain any useful information.
Test programs
fd-close-twice.c:
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
int main() {
int fd = open("foo.txt", O_RDONLY | O_CREAT);
printf("fd = %d\n", fd);
assert(fd != -1);
close(fd);
close(fd);
return 0;
}

lseek-invalid-fd.c:
#include <stdio.h>
int main() {
lseek(-1, 0, SEEK_CUR);
return 0;
}

(the pop-up dialog boxes are issued when linking against the debug version of the UCRT using cl from VS2019)
I also tried to define __MINGW_SHOW_INVALID_PARAMETER_EXCEPTION while compiling mingw-w64-crt, but to no avail.
https://github.com/mingw-w64/mingw-w64/blob/2938ef65959f0f6f56070f725a05b9d57a8920ac/mingw-w64-crt/crt/crtexe.c#L119-L122
link against debug version of the UCRT using this toolchain?
Do you mean ucrtbased.dll? In mingw-w64 UCRT, programs are not directly linked with ucrtbase.dll but with APISet DLLs.
link against debug version of the UCRT using this toolchain?
Do you mean ucrtbased.dll? In mingw-w64 UCRT, programs are not directly linked with ucrtbase.dll but with APISet DLLs.
The import library libucrt.a links against the apiset dlls, but you can also use libucrtbase.a which links against ucrtbase.dll directly.
Assuming that ucrtbase.dll and ucrtbased.dll export pretty much the same symbols, you can make a copy of ucrtbase.def.in and change it to say “LIBRARY ucrtbased.dll”, hook it up in Makefile.am like the existing one, regenerate Makefile.in, rebuild. Then in clang-based setups you could add -lucrtbased to make it link against that instead.
Assuming that ucrtbase.dll and ucrtbased.dll export pretty much the same symbols, you can make a copy of ucrtbase.def.in and change it to say “LIBRARY ucrtbased.dll”, hook it up in Makefile.am like the existing one, regenerate Makefile.in, rebuild. Then in clang-based setups you could add -lucrtbased to make it link against that instead.
Ah, interesting. Let me try that. If that works, would the Mingw-w64 project be interested in an option that does this thing automatically? For example, when configuring with --with-default-msvcrt=ucrtd (or --with-default-msvcrt=ucrtbased).
I thought that option was not there due to licensing issues, but could not find any further information on this.
Assuming that ucrtbase.dll and ucrtbased.dll export pretty much the same symbols, you can make a copy of ucrtbase.def.in and change it to say “LIBRARY ucrtbased.dll”, hook it up in Makefile.am like the existing one, regenerate Makefile.in, rebuild. Then in clang-based setups you could add -lucrtbased to make it link against that instead.
Ah, interesting. Let me try that. If that works, would the Mingw-w64 project be interested in an option that does this thing automatically? For example, when configuring with
--with-default-msvcrt=ucrtd(or--with-default-msvcrt=ucrtbased).I thought that option was not there due to licensing issues, but could not find any further information on this.
Not sure how others feel about it. As that DLL isn’t available preinstalled anywhere, it’s a bit off from the general target. It would still be ok license-wise to distribute that def file and import library with mingw-w64, but not to distribute apps built with it (in general).
Here's a patch that allows mingw-w64-crt to be configured with --with-default-msvcrt=ucrtbased:
https://github.com/libvips/build-win64-mxe/blob/51a275dd7a4888a67c6bbd08ac448505da2bbde0/build/patches/mingw-w64-9-ucrtbased.patch
Which seems to work for me. ~Note that it's not necessary to install the Windows Universal CRT SDK to obtain ucrtbased.dll. It seems that it is preinstalled in C:\Windows\System32\ with Windows 10 x64. However, I didn't check if this is also the case on the other architectures.~ Edit: After further checking on a few other Windows systems (including ARM64), this was not the case. I'm not sure how ucrtbased.dll ended up in that directory.