[PATCH] D80880: [clang] [MinGW] Link kernel32 once after the last instance of msvcrt

Martin Storsjö via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 16 23:57:46 PDT 2020


mstorsjo added a comment.

In D80880#2066938 <https://reviews.llvm.org/D80880#2066938>, @amccarth wrote:

> Yowza.  Mingw still links against MSVCRT?!


Sorry, I missed this comment earlier - that I see that I want to reply to.

Traditionally, mingw still links against the OS private msvcrt.dll yes. But nowadays it's also possible make it target UCRT instead. As the default `-lmsvcrt` is pretty deeply ingrained in both GCC and Clang, and there's no very convenient method of changing it that works on both compilers(*), the approach taken for using other CRTs, is to install the chosen default CRT under the name of `libmsvcrt.a` so that all tools use it by default. (The import library specifically for the system's msvcrt.dll is also available under the name `libmsvcrt-os.a` for disambiguation.) So in that context, `-lmsvcrt` no longer specifically means msvcrt.dll, in practice it just means "the default CRT".

As changing from one CRT to another affects a lot of the built code (e.g. CRT structures have different layout, inline functions in headers redirect calls to different physical functions in the CRT, etc), the most safe way of changing it is to pick a different default when building a mingw toolchain from scratch, which can be done by passing `--with-default-msvcrt=ucrt` when installing/building mingw-w64-headers and mingw-w64-crt.

*) With GCC, to change the default linked CRT, one can dump the built-in spec files, edit it to change `-lmsvcrt` into something else, and then build by passing that custom spec file to GCC with `-spec myspecfile`. MSYS2 carries (or at least used to carry) a non-upstream GCC patch adding a custom option for overriding the name of the default CRT. With Clang, we have logic that looks for linked libraries named `-lmsvcr*` or `-lucrt*`. If any such one is present on the link command line, the default implicit `-lmsvcrt` is omitted. (This doesn't place the custom CRT libs in the exact same spot in the link command line, but that doesn't matter very much for ld.lld, only for ld.bfd.)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D80880/new/

https://reviews.llvm.org/D80880





More information about the cfe-commits mailing list