-->

How to circumvent Windows Universal CRT headers de

2019-01-25 12:51发布

问题:

In trying to evaluate Clang on Windows, utilizing the Windows Universal C Run-Time (...\Windows Kits\10\Include\10.0.15063.0\ucrt) I was immediately facing unexpected wall, in the form of an undisclosed and unexpected dependency on Microsoft's Visual Studio. Apparently even the simplest C program will not be able to compile as soon as you include any standard C header, because they all seem to end-up attempting to #include vcruntime.h (which is not part of the UCRT).

My questions are:

  1. Is there a way to utilize the Windows Universal C RTL SDK withOUT Visual Studio?
  2. If it is not intended or possible, why then is it not called "Windows CRT for Microsoft VC" - what am I missing?

回答1:

Check out [MSDN]: Introducing the Universal CRT (and also the other URLs that it references):

In June of last year we published a pair of articles discussing the major changes that we had made to the Visual C++ C Runtime (CRT) for Visual Studio 2015.
...
The AppCRT and DesktopCRT have been recombined into a single library, which we have named the Universal CRT. The new DLLs are named ucrtbase.dll (release) and ucrtbased.dll (debug); they do not include a version number because we’ll be servicing them in-place.

From [MSDN]: The Great C Runtime (CRT) Refactoring (to clear things up about (some of) the bold items):

In order to unify these different CRTs, we have split the CRT into three pieces:

  1. VCRuntime (vcruntime140.dll) ...

  2. AppCRT (appcrt140.dll) ...

  3. DesktopCRT (desktopcrt140.dll) ...

According to [MS.Support]: Update for Universal C Runtime in Windows:

Microsoft Visual Studio 2015 creates a dependency on the Universal CRT when applications are built by using the Windows 10 Software Development Kit (SDK).

and from [MS.Dev]: Windows 10 SDK:

Note: Windows 10 development targeting Windows 10, version 1803 (or later) requires Visual Studio 2017. This SDK will not be discovered by previous versions of Visual Studio.

So, UCRT is strictly bound to VStudio. Universal: means that it's not dependent on VStudio version (all VStudio versions will use a common one (there can be only one)).

UCRT is the Win (wannabe) equivalent of Ux's libc.

I took a look in SDK include dir (e.g. "c:\Program Files (x86)\Windows Kits\10\Include\10.0.15063.0\ucrt"):

  • Every common file (e.g. stdio.h) has an #include <corecrt.h>
  • corecrt.h has an #include <vcruntime.h>

no #ifdefs, so there is no way (at least no easy one) to overcome this.

But, things are even clearer when reaching link phase. If your C code includes UCRT headers, it will (most likely) link to files from SDK lib dir (e.g. "c:\Program Files (x86)\Windows Kits\10\Lib\10.0.15063.0\ucrt\x64"), which are generated by VStudio, and there's a great chance for that to fail. Example:

code.c:

//#include <stdio.h>


int main() {
    //printf("Dummy.... sizeof(void*): %d\n", sizeof(void*));
    return 0;
}
e:\Work\Dev\StackOverflow\q045340527>dir /b
code.c

e:\Work\Dev\StackOverflow\q045340527>"c:\Install\x86\Microsoft\Visual Studio Community\2015\VC\bin\cl.exe" -nologo -c -Focode.obj code.c
code.c

e:\Work\Dev\StackOverflow\q045340527>"c:\Install\Google\Android_SDK\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang.exe" -c -o code.o code.c

e:\Work\Dev\StackOverflow\q045340527>dir /b
code.c
code.o
code.obj

The 2 (generated) files are incompatible:

e:\Work\Dev\StackOverflow\q045340527>"C:\Install\x86\Microsoft\Visual Studio Community\2015\VC\bin\amd64\dumpbin.exe" -nologo code.obj

Dump of file code.obj

File Type: COFF OBJECT

  Summary

          80 .debug$S
          2F .drectve
           7 .text$mn

e:\Work\Dev\StackOverflow\q045340527>"C:\Install\x86\Microsoft\Visual Studio Community\2015\VC\bin\amd64\dumpbin.exe" -nologo code.o

Dump of file code.o
code.o : warning LNK4048: Invalid format file; ignored

  Summary


e:\Work\Dev\StackOverflow\q045340527>"c:\Install\x64\Cygwin\Cygwin\AllVers\bin\readelf.exe" -d code.o

e:\Work\Dev\StackOverflow\q045340527>"c:\Install\x64\Cygwin\Cygwin\AllVers\bin\readelf.exe" -d code.obj
readelf: Error: Not an ELF file - it has the wrong magic bytes at the start

Now, I know that lld (I remember that I built it in the past, but I can't find it, in order to test my statement) is able to link both ELF and COFF file formats, but I doubt that it can combine them.

Conclusion

Based on the above, here are the answers to your questions:

  1. I suppose it is - an unsupported one though (claiming that something is impossible is almost always false). But, there would be lots of restrictions (consider the above file format matching), and would most likely require some dirty tricks or workarounds (gainarii) like (some that I can think of now):

    • Altering it (editing its header files - to remove the unwanted #includes)
    • Creating a dummy vcruntime.h file (to get past the compile phase)
  2. Adding VStudio (or anything else, as a matter of fact) in the name would automatically decrease its "universality level".
    And this is only the 1st step: it has been separated from VC Runtime. Think of it as of a baby. In time, it will become mature and (more stable) and maybe other compilers / build toolchains will end up supporting it (no need to follow the spartan rules, and throw it off the cliff :) ... at least not right now).
    But, I think only MS could have an answer to this (although there's a great chance that they won't provide a clearer one)



回答2:

I live in a country with a television network called Global. For the first 15 years of Globals existence, you could only watch it if you lived in a one city, Toronto. Yet, it was Global. Microsoft has a similar definition of Universal.

In the real programming world, there is a better definition of universal, it is called UNIX. Even Microsoft has been forced to (pretend to) understand UNIX; and Linux definitely understands UNIX. You don’t have to put up with shitty development tools; Linux and UNIX are available everywhere.