Clang/LLVM Support in Visual Studio

Will Buik

Will

Visual Studio 2019 version 16.1 Preview 2 comes with support for Clang/LLVM out-of-the-box. Visual Studio has had great tooling for MSVC and GCC for quite a while now. The latest preview brings Clang into the fold.

Visual Studio 2019 includes out of the box support for editing, building, and debugging CMake projects with Clang/LLVM. If you use MSBuild projects, however, don’t worry. Support for MSBuild based .vcxproj projects is coming soon as well. If you are not familiar with Visual Studio’s CMake, support check out how to get started.

If you are developing on Windows and targeting Clang, we hope to make Visual Studio the best IDE for that job. Please try out the preview and let us know what is working well and what isn’t.

Installing Clang/LLVM for use with Visual Studio

The “Desktop development with C++” workload with the Visual Studio installer now includes full support for targeting Clang/LLVM based toolsets. You will also need the actual compiler and tools to build your projects.

Getting Clang on Windows

On Windows, it’s easy to install the Clang tools. Just grab the “Clang compiler for Windows,” an optional component of the “Desktop development with C++” workload. This will install everything you need to develop with Clang on Windows.

Install the “Clang compiler for Windows” optional component as part of the “Desktop development with C++” workload.

You can also install your own copy of Clang/LLVM or even build it from source. If you have already installed Clang/LLVM you don’t need to install the compiler again with the Visual Studio installer. We do recommend that you use the most recent version of Clang to get the best support in the IDE. Older versions may have some limitations. Check out “Using a Custom Version of Clang” below for more information.

Getting Clang on Linux

To use Clang/LLVM on a remote Linux machine with Visual Studio, just install it with your distribution’s package manager. If ‘which clang’ finds the compiler you are good to go.

Getting Started with Clang/LLVM in Visual Studio

Once you have installed Clang, using it with CMake projects is easy. Visual Studio will use the MSVC compiler by default on Windows. On Linux, it will use the distribution’s default compiler, often GCC. To use Clang instead, add a configuration and select one of the Clang presets.

Select “Manage Configurations” from the configuration drop down on the left of the play button. Then click the “Add Configuration” button:

Clang for Windows CMake configuration presets.

Another option is to modify an existing configuration with the CMake Settings editor to use any of the “clang” toolsets with the Toolset dropdown:

These toolsets will use Clang in clang-cl mode by default on Windows and link with the Microsoft STL. If you are targeting Linux, they will use Clang in GCC compatibility mode. Toolsets to build both 32 and 64-bit binaries are available on Windows while the Linux toolsets will target the architecture of the remote machine. Most of this behavior can be tweaked by customizing either the CMake command line or in the CMake project itself.

Edit, Build, and Debug

Once you have set up a Clang configuration, build and debug work exactly as you would expect in the IDE. Visual Studio will automatically detect that you are using the Clang compiler and provide applicable IntelliSense, highlighting, navigation, and other editing features. Building projects should just work, provided they are compatible with Clang, with any errors or warnings being directed to the Output Window.

Whether you are targeting Windows or Linux, the debugging experience with Clang should be familiar to you. Most of Visual Studio’s debugging features also work with Clang. There are just a few exceptions for compiler dependent features (e.g. edit and continue.) Breakpoints, memory and data visualization, and other inner development loop debugging features are available:

Using a Custom Installation of Clang

Visual Studio will automatically look for Clang in two places:

  1. (Windows) The internally installed copy of Clang/LLVM that comes with the Visual Studio installer.
  2. (Windows and Linux) The PATH environment variable.

If it doesn’t find Clang in one of those places it will offer to install it on Windows. On Linux, you will need to install it with the distribution’s package manager. Alternatively, you can tell Visual Studio to use another installation of Clang/LLVM on the machine by setting the “CMAKE_C_COMPILER” and “CMAKE_CXX_COMPILER” CMake variables in your CMake Settings:

Set “CMAKE_C/CXX_COMPILER” variables with the CMake Settings editor to use a custom Clang installation.

Keep in mind, however, that using old versions of Clang may come with some limitations. For instance, if you are using the Microsoft STL on Windows, support is only gaurenteed for the most recent version of the compiler. As we release new versions of Visual Studio and the Microsoft STL you may need to upgrade your custom installation.

Using Clang/LLVM with MSBuild Projects

Visual Studio 2019 version 16.1 ships with out of the box support for Clang/LLVM for CMake projects. Support for MSBuild based C++ projects is on the roadmap and will be available soon in a future release. To use Clang/LLVM today with MSBuild based C++ projects there are 3rd party extensions for Visual Studio available.

Send Us Feedback

Please try out the latest preview and let us know if you have any feedback. It is always appreciated! The best way to get in touch with us about an issue or suggestion is though Developer Community with the “Report a Problem” or “Suggest a Feature” tools. This makes it easy for us to follow up and for you to get the latest updates about our progress. Feel free to comment here or send an email to cmake@microsoft.com with questions as well.

27 comments

Comments are closed. Login to edit/delete your existing comments

  • Avatar
    Roman Petrov

    Do you have any plans to upgrade LLVM distribution within Visual Studio to latest LLVM 9.0 release?

  • Avatar
    Edward Lambert (SI)

    Any idea why it is 32bit clang binaries that are shipped with the VS installer? This means I cannot use LTO which is my entire reason for wanting to use clang, if I replace the bin & lib versions from the installer from the LLVM website, which are the 64bit versions it works fine without running out of memory, but this isn’t relaly great for deployment on scale as we have to do some kind of custom setup.

    Is there plans to ship the 64bit versions by default please, if not I will add a bug / feature request ?

  • Avatar
    shbouwhuis@gmail.com

    Just so I understand this correctly:
    1. I can use Visual Studio 2019 on Windows and use Clang to compile and link Windows projects/binaries/DLLs?
    2. I can use Visual Studio 2019 on Windows and use Clang-tidy to find problems for Windows projects/binaries/DLLs?
    3. I can use Visual Studio 2019 on Windows and use Clang both for 32-bit and 64-bit Windows projects/binaries/DLLs?

    • Will Buik
      Will BuikMicrosoft employee

      1 and 2: Yes.
      3. You can target both 32-bit and 64-bit Windows binaries/DLLs with the included version of Clang. The included version of Clang itself is a 32-bit binary, but it can still build binaries that target 64-bit Windows. If you need the 64-bit version of Clang you can install it and Visual Studio can use it instead.

  • Avatar
    Yi E Zhu

    I have a simple program which can’t build with LLVM toolset. Looks like the symbol “__udivti3” was in the compiler-rt library, but i don’t known how to link with it.

    Visual Studio Community 2019, Version 16.5.1

    #ifdef __clang__
    typedef int int128_t __attribute__((mode(TI)));
    typedef unsigned uint128_t __attribute__((mode(TI)));
    uint128_t div128(uint128_t x, uint128_t y)
    {
    return x / y;
    }
    #endif

    int main()
    {
    return 0;
    }

    1>—— Build started: Project: ConsoleApplication1, Configuration: Release x64 ——
    1>lld-link : error : undefined symbol: __udivti3
    1>>>> referenced by C:\Users\zhuyie\source\repos\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp:9
    1>>>> x64\Release\ConsoleApplication1.obj:(?div128@@YA_M_M0@Z)
    1>Done building project “ConsoleApplication1.vcxproj” — FAILED.
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========