Clang/LLVM Support in Visual Studio

Avatar

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.

Avatar
Will Buik

Follow Will   

9 Comments
Avatar
Konstantin __ 2019-05-15 12:27:52
Please search for clang-format.exe ,clangd.exe and any clang related executable as well in the PATH before running the included one fromC:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\VC\vcpackages\clang-format.exe
Avatar
David Hunter 2019-05-01 18:51:05
I was wondering if you had given any thought to letting the standard c++ library be selectable independent of compiler. My undertanding is that the gcc/clang/cl compilers can use libc++/libstdc++/MSVC standard library in maybe not all but most combinations.  
Avatar
Michael Winterberg 2019-04-30 18:38:52
Instead of doing remote compilation, has there been any thought using clang locally to cross compile to Linux instead of doing remote compilations? Presuming that all headers / libraries / whatever else are already local. Specifically with the MSBuild integration?
Avatar
Park Dong Ha 2019-04-28 05:37:16
It worked pretty well with VS 2019! :DI suggest the post to add how to install LLVM packages via chocolatey(https://chocolatey.org/) shortly. It installs latest clang-cl and config PATH automatically.
Hans Wennborg
Hans Wennborg 2019-04-25 08:16:14
This is very exciting for us. In particular I'm looking forward toseeing an official msbuild toolset so we can deprecatehttps://marketplace.visualstudio.com/items?itemName=LLVMExtensions.llvm-toolchainAny plans on adding lld support to the toolset and installer?Also was there any reason you didn't include the more recent 8.0.0release in the installer, or just timing? As release manager I'd bekeen to know if you saw any issues with that release, or if you wantto be involved in the process of future releases.Congratulations on this work, and thanks for a good blog post! (I tried emailing the address in the post, but it bounced.)
Avatar
Alastair Taylor 2019-04-25 07:10:20
“To use Clang/LLVM today with MSBuild based C++ projects there are 3rd party extensions for Visual Studio available” It could be worth linking to these 3rd party extensions