Bringing Clang to Windows

As you may know, Visual Studio now supports building Android and iOS applications using Clang. We realize the need of our users to write cross-platform apps and we know that C++ offers massive benefits in the cross-platform arena. Clang has made impressive strides in standards conformance and diagnostics and is fast becoming the preferred C++ compiler for non-Windows platforms. In order to extend the benefits of Clang to C++ developers building apps for Windows, we have been working on an experimental implementation of Clang on Windows. First preview of that was demonstrated in this //build talk by Jim Radigan.

The Clang and LLVM community has already done tremendous work towards providing a toolchain that builds C++ code for Windows.  This blog post first announced it.  Subsequent progress was also noted through other posts such as this and this.  They have also provided neatly packaged toolset builds for Windows that integrate very nicely with Visual Studio.  Our work builds on top of all the impressive work that the community has already done.

On our end, the primary motivation for doing this work is to enable the cross-platform scenario where a significant portion of your app is cross-platform C++ code.

If you already build cross-platform apps, you probably have a code-base that somewhat resembles the above structure. Here (1) and (3) are platform specific pieces of code, either because they call into OS-specific API or they happen to use programming language features that are provided only by some platform specific compiler tool-sets. There are usually genuine reasons for such code to exist. However, at the same time, a big chunk of your app logic is (or can be) written using proper standards conformant cross-platform C++ code. This is the block marked (2) above.

Imagine an app whose code structure resembles the above diagram being built for various platforms:

  1. For Android and iOS, the entire apps would be built using Clang
  2. For Windows, the entire app would be built using Visual C++

This essentially means that, even though we are calling the block (2) cross-platform, we are still building it using different compilers for different platforms. So, any minor divergence in features of Clang and Visual C++ will cause you as a developer to have to worry about these compiler differences or limit you to using the smallest subset of the two compiler features. This is definitely not ideal.

  • What if you could use a single compiler for your cross-platform code irrespective of what platform you target?
  • What if you could still use a separate compiler to build all of your platform specific (or compiler specific) code that you have accumulated over the years?
  • What if you could have rich inter-op between the above two while still making use of the excellent, secure, debugging-friendly code-generation engine that powers Visual C++ and builds some of the most complex software on Windows (almost everything.. including Windows!)?

This is now possible with the work we have done. What this enables is a scenario in which you compile (1) and (3) using the Visual C++ compiler, (2) using Clang and for code-generation, all of these would still use the Visual C++ back-end (We call it C2).  All of this while still enjoying the rich end-to-end developer experience within Visual Studio.

We believe using our back-end as the core of this technology gives us a lot of benefits since we look at C2 and the linker as a part of the ABI of the Windows platform.  Having dealt with code-generation for all supported processor architectures under various conditions, we believe it is best placed to power the new technologies like .NET Native and now Clang (for C++ and even for ObjectiveC).

This is work in progress and we will be talking a lot more about this work in coming months. As always, we look forward to your feedback on this scenario and our approach.

Thank you

Visual C++ Team