3

I would like to be able to use mingw-w64 to build binaries that link to the Universal CRT available with Visual Studio 2015+ instead of MSVCRT.lib. The release notes for mingw-w64 v6.0.0 include the following: "Massive additions to support UCRT thanks to Martin Storsjö"

Unfortunately my searches have not revealed the documentation on how to use this support.

Does anyone know what options I need to supply and where to supply them?

  • mingw-w64 uses gcc which does not link with msvcrt (proprietary). – Biswapriyo Aug 22 '19 at 13:02
  • @Biswapriyo - I'm aware this has been the case in past versions of mingw-w64, but the release notes for the latest version (linked in the question) mention UCRT support. –  Aug 23 '19 at 18:21
  • 1
    I haven't actually tried it, but there is some information at the [mingw-w64 mailinglist](https://sourceforge.net/p/mingw-w64/mailman/mingw-w64-public/thread/12e05577525941149af35a3e5cb7fa66%40vector.com/). – ssbssa Aug 27 '19 at 10:49
  • `$ gcc -mcrtdll=ucrt ...` worked for **linking** against the UCRT but the option doesn't define any of the `_UCRT*` macros. – diapir Jun 26 '20 at 11:04

1 Answers1

8

Well, I have done it along with VS 2017. But as far as I understand both VS 2015 and VS 2017 use the VCRUNTIME140.DLL, so no worries here.

It is divided into two steps:

1. Creating an import library for VCRUNTIME140.DLL:

This is done by doing the following:

mkdir scratch; cd scratch
cp C:/Windows/System32/vcruntime140.dll .
dumpbin /exports vcruntime140.dll > exports.txt
echo LIBRARY VCRUNTIME140 > vcruntime140.def
echo EXPORTS >> vcruntime140.def
tail +20 exports.txt | head -n -10 | awk '{print $4}' >> vcruntime140.def
lib /def:vcruntime140.def /out:libvcruntime140.a /machine:x86
cp libvcruntime140.a $(MINGW_ROOT)/i686-w64-mingw32/lib

2. Change the way MinGW GCC operates to link against VCRUNTIME140 and UCRT instead of MSVCRT

gcc -dumpspecs > $(MINGW_ROOT)/lib/gcc/i686-w64-mingw32/$(GCC_VERSION)/specs
  1. Add -D_UCRT to cpp and cc1plus sections of the specs file. This will prevent undefined references linking errors for the scanf functions family. Check my other question.
  2. Replace -lmsvcrt to -lvcruntime140 -lucrt in the libgcc section of the specs file.

Please exchange $(MINGW_ROOT) with the location of where u have MinGW.

Notes:

  • The platform signature part, i686-w64-mingw32, within the paths I included may differ for your case. I believe based on the architecture. So you may have to modify that accordingly.
  • You need to use a relatively new MinGW that has libucrt.a in the $(MINGW_ROOT)/i686-w64-mingw32/lib folder. I am using MinGW 7.0.0 with GCC 7.4.0.
Ayman Salah
  • 1,039
  • 14
  • 35
  • 1
    Two notes about this: 1) You don't need to use vcruntime140.dll, so the whole step 1 can be skipped. Just remove `-lvcruntime140`, `-lucrt` is enough. vcruntime140.dll isn't shipped as part of the UCRT itself but can be bundled with the app (or may or may not be installed somewhere in a system directory) 2) When compiling C code and not linking any prebuilt static libraries (outside of the default libmingw32.a and libmingwex.a) this works, but other prebuilt libraries can depend on details that differ significantly between CRT versions. – mstorsjo Jun 15 '20 at 20:20
  • 5
    In particular - libstdc++ probably depends a lot on the CRT it was built for - so you can't use a prebuilt libstdc++ that was built for msvcrt.dll and link it against UCRT. (Or it may seem to work and give tricky latent bugs.) So this approach of adding `-D_UCRT` and replacing `-lmsvcrt` with `-lucrt` works if you don't use prebuilt libraries including libstdc++ and build everything else from scratch. The safest approach is to rebuild the whole toolchain from scratch with `--with-default-msvcrt=ucrt` passed to mingw-w64-headers and mingw-w64-crt. – mstorsjo Jun 15 '20 at 20:22
  • 1
    Yes, exactly I have built MinGW crt and headers with `--with-default-msvcrt=ucrt` and used them during compilation of GCC as well as all the libs that I have added afterwards. About vcruntime140 yes it is not necessary. – Ayman Salah Jun 16 '20 at 12:47