mingw-w64universal-crt

How do I build against the UCRT with mingw-w64?


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?


Solution

  • 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: