Clang/LLVM Support for MSBuild Projects

Will Buik

Visual Studio 2019 version 16.2 Preview 3 includes built-in Clang/LLVM support for MSBuild projects. In our last release, we announced support for Clang/LLVM for CMake. In the latest Preview of Visual Studio, we have extended that support to also include MSBuild projects. While in most cases we recommend using the MSVC compiler, we are committed to making Visual Studio one of the most comprehensive IDEs on Windows. You may want to use Clang instead if you are developing cross platform code, especially if it already depends on Clang or GCC extensions. You can now use Clang/LLVM to target both Windows and Linux using MSBuild just like you can with CMake projects. We’ve also updated our included version of Clang to 8.0.0. Please download the latest Preview to try it out and let us know how it works.

Installing the Clang Tools for Visual Studio

You can install the Clang tools for Windows by selecting “C++ Clang Tools for Windows” as part of the “Desktop development with C++” workload. It is not installed by default, but if you have installed it before, Clang will automatically be updated to 8.0.0 when you install the latest Preview.

Install the “C++ Clang tools for Windows” component with the “Desktop development with C++” workload.

If you want to use your own Clang compiler with Windows instead of the bundled one, you can do that too. Navigate to “Individual Components” and select “C++ Clang-cl for v142 build tools.” You will only be able to use recent versions of Clang (8.0.0 or later) with the Microsoft STL though. We strongly recommend using the bundled compiler as it will be kept up to date as the STL is updated.

Or just install the tooling with “C++ Clang-cl for v142 build tools” under “Individual Components.”

To use Clang with Linux projects, just install the “Linux development” workload. You won’t need to select any more components. The remote machine or WSL will need to have Clang installed. Just install Clang from your distribution’s package manager or from LLVM’s download page.

Use Clang with Windows MSBuild Projects

You can use Clang with most MSBuild projects that target Windows. To get started, create a new C++ project or open an existing one. Then, you can change the platform toolset to “LLVM (clang-cl)”:Select the “LLVM (clang-cl)” Platform Toolset under Configuration Properties > General.

If this toolset doesn’t appear, it likely isn’t installed – see above for installation instructions.

Visual Studio uses the clang-cl frontend with MSBuild on Windows so the properties for Clang will be the same as MSVC based projects. Some compiler options are not supported by clang-cl (e.g. Just My Code) and will be not be shown in the Property Pages when you are using Clang.

Use Clang with Linux MSBuild Projects

Using Clang with Linux projects is also as simple as selecting the appropriate platform toolset. For Linux projects, there are two toolsets to choose from. One for using Clang with a WSL instance on the local machine and another for using Clang on a remote machine:

For Linux projects, Visual Studio uses the Clang GCC-compatible frontend. The project properties and nearly all compiler flags are identical.

Custom Clang Installations and Compiler Arguments

You can also use a custom installation of Clang. On Windows, by default, the built-in version of Clang from the installer will always be used. On Linux, the first installation of Clang found on the PATH will be used. However, you can override this behavior on either platform by setting defining a property in your project file:

<LLVMInstallDir>PATH_TO_LLVM</LLVMInstallDir>

To do this, you will need to unload your project and edit it. You can add this to any project configurations that you would like to use your custom installation of Clang. Keep in mind, the Microsoft STL is only compatible with very recent versions of Clang: 8.0.0 as of this post.

If you need to use a Clang compile or link flag that isn’t supported by the project property pages, you can do that in the project properties under Configuration Properties > C/C++ or Linker > Command Line. Consider opening a feedback ticket if you find yourself using a particular option this way frequently. Based on demand, we may add it to the property pages.

Send us Feedback

Your feedback is a critical part of ensuring that we can deliver the best experience.  We would love to know how Visual Studio 2019 version 16.2 Preview 3 is working for you. If you find any issues or have a suggestion, the best way to reach out to us is to Report a Problem.

7 comments

Discussion is closed. Login to edit/delete existing comments.

  • Park Dong Ha 0

    Glad for the update 😀 Can we get the path of the `clang-cl`? I’d like to update my `CMakeSettings.json` for CMake integration to use the path.

  • Khufu_I 0

    Thank you for continuing to invest and add Clang and CMake support in Visual Studio, it’s really awesome. Is it possible to use clang++ in addition to clang-cl in MSBuild or CMake project? It would make it easier to use the same compiler flags across different platforms.

  • Terminator T800 0

    Do we still need to install the Windows 10 SDK to compile a simple console app with LLVM as it’s currently the case with MSVC? 

  • Brent Farris 0

    Alas! I can finally write _Generic in C since regular VS compiler refuses to fully support C1x. The only problem I have now is that the build time is incredibly slow and Multi-Processor Compilation is now missing so I can’t make it any faster? Also I no longer see the compilation output other than it has started, I’m assuming I need to pass some flag to print out what is going on or something.

    I know it sounds like a lot of complaining, but I’m very greatful that we now have this option because I haven’t had time to do all the CMake stuff (I just use a makefile for Linux/OSX building). Thanks again and if you have any solutions to the following problems please let me know!

    1) Slow build times

    2) Compilation output missing

  • R-J Wolthuis 0

    LLD cannot link some binaries build in vcpkg (gtest). How can I configure the MSBuild project to use link.exe for those builds?
    With CMake there is no issue, since then link.exe is the default.

    • R-J Wolthuis 0

      It turned out that with `lld-link.exe` it is necessary to add `gtest.lib` or `gtestd.lib` to Linker > Input > Additional Dependencies, in the MSBuild configuration.

      Note that when using the LLVM extension this issue also occurs when trying to link with `lld-link`, but not with `link.exe`.

      An option in the MSBuild project to choose what linker to use would still be appreciated.

Feedback usabilla icon