Bringing Clang to Windows

Bringing Clang to Windows

Rate This
  • Comments 11

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

Leave a Comment
  • Please add 4 and 4 and type the answer here:
  • Post
  • Will it be possible to use Clang to compile (1) and (3) too?

  • Eddie, current focus is box 2.   we want to significantly reduce the friction to making cross plat code work well.  

  • > and is fast becoming the preferred C++ compiler for non-Windows platforms.

    This appears to be wishful thinking. Clang is standard for Mac, GCC in all its GPLv3 goodness is standard for Linux.

    You've made a falsifiable claim here. What are your numbers that support the claim you're making?

  • @ David Gerard

    How about Android and iOS ?

    Also more and more linix distros support clang too.

  • Guess i'll have to wait until Google finishes the working their doing to bring Clang to Windows (that targets the MS ABI). Meanwhile i'll stick to Mingw-w64, i can use Clang with it.

    Anyway, thanks for the quick response Steve.

  • That sounds great! Nicely done!

    One question: if the Microsoft linker is considered part of the Windows API, any chance of getting it to support reproducible builds? Zeroing out timestamps etc.?

  • Eddie, I want to make sure I understand your feedback.   Why the preference for a Google version?   We are just trying to say that we are focusing our efforts on portable code for Clang/C2.  If your code doesn't have MS extensions, you should be able to use it.

  • I like the above, and as a first step, building my cross-platform code with Clang would be a gigantic help. But here's some elucidation of the pushback you are seeing.

    The ask is that we can build Windows binaries start to finish with Clang, for multiple reasons. For the foreseeable future, Clang tracks the C++ standard quicker than Microsoft (I'd like C++14 constexpr, and so on). But it would also be cool to build cross-platform binaries on a single machine, instead of using multiple machines. I can build Android binaries on Windows, for example, and this is cool. I'd like to build Windows binaries on a Linux or Mac box.

    I love the Visual Studio IDE, and the compiler and linker have always been fast compared to the competition, and usually generate much better code to boot. But I don't like the slowness in getting new features in the language or libraries. Hence - Clang.

    If Visual Studio would build Mac and Linux binaries and it was bleeding edge in terms of language and library support, I'd use it for everything.

  • > ... If your code doesn't have MS extensions, you should be able to use it.

    Yeah my code, which only targets Windows at the moment, doesn't use any MS extensions. So if i understand it right, as long as my code doesn't use any MS extensions, i should be able to build a native Windows executable with Clang.

    That's great then. I'm looking forward to try it.

    Thanks.

  • I'm wondering WHEN or if it's NEVER the Clang would support compiling COM/ATL code...

  • Re: Brian Fitzgerald – you are totally right-on. Clang is by far the most forward-looking compiler for C++ and a pleasure to use. Given Herb Sutter's wonderful evangelically effusive advocacy of writing "Modern C++" (his term, which I completely agree with) and using C++ in a truly portable fashion, it's a shame to see MS still weighed down by obligations to the visual compiler in this day an age. I applaud these new moves by Microsoft, bravo to them – but I feel like you have to commit all the way to be meet the promise. Support vc completely of course, get optimisations for the Windows platforms of course, but I would expect in the contemporary context that you should be able to write C++ which compiles with clang anywhere and compiles with clang in Visual Studio just the same for any platform including Windows, and the optimisations from writing vc++-compliant code should be done only _when necessary_ and at as late a stage of development as possible  – if that means MS has to throw resources at LLVM and clang then that's what needs to happen, and the results will be better for everyone including MS in the long term. In the same spirit as Herb's "Conformance And Cross-Platform Mobile Development" (channel9.msdn.com/.../311)  pitch for VS 2015, how can C++ be portable with the need for vc-compliant code when building for MS products? Surely that part should be optional rather than a requirement? Xamarin is great, but if you are pitching yourself into the same space in the context of C++ instead of C# then there are obviously a lot more steps to be taken.

    I'm a young developer who loves C++ but I'm not a Windows dev so I'm not trying to proclaim any laws at all, I just see myself as precisely one of the targeted market demos for switching to VS2015 and these are just the obvious thoughts that arise for me. I watched Herb's cross-platform presentation and feel like that's the gulf between the 'promise' and the reality.

    But still, bravo!

    Re: Eddie – exactly. Windows binaries with clang out of the box is the expectation. Let us know how it works.

    p.s. I had to remote into my Win8 box at home to post this as it didn't like Safari on my Mac laptop. Guess the leopard can only change it's spots so quickly!

Page 1 of 1 (11 items)