Content outdated
For up-to-date documentation see Clang/LLVM support in Visual Studio projects.For an overview of the Visual Studio capabilities described in this article, see Develop C and C++ applications.
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.
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:
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:
- (Windows) The internally installed copy of Clang/LLVM that comes with the Visual Studio installer.
- (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:
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.
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...
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?
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.
Thanks! Yes to question 2 makes me VERY happy!!!
Since you are shipping only with the 32-bit version (sad face), should I install Clang 64-bit myself, or is it better to wait for it to be included in Visual Studio itself?
PS
I upvoted https://developercommunity.visualstudio.com/idea/806348/distribute-x64-builds-of-clang.html.
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...
We are considering shipping 64-bit versions of Clang. There is already a suggestion here (https://developercommunity.visualstudio.com/idea/806348/distribute-x64-builds-of-clang.html) but please vote or comment if you are interested. It helps us prioritize features like this!
Do you have any plans to upgrade LLVM distribution within Visual Studio to latest LLVM 9.0 release?
Take a look at the Visual Studio 2019 16.4 Preview…
Thank you, I will!
It’s better to mention that the toolset name working with cmake is “ClangCL”. I’ve tried clang-cl, clang, llvm. None of them works untils I’ve found this page https://tracker.dev.opencascade.org/view.php?id=31060 mentioning the “ClangCL”
And for 64-bit builds, set the CMake toolset to ‘ClangCL,host=x64’
https://cmake.org/cmake/help/latest/variable/CMAKE_GENERATOR_TOOLSET.html
Does it support OpenMP 4.5 as CLang?
I really hope this integration will give us a more modern OpenMP library.
Yes, I’ve tested it with OpenMP 4.5 code and it works.
https://twitter.com/_tim_hutton_/status/1224463399138025472
Seems that msbuild launches just one clang.exe at a time
I assume the /MP option doesn;t work ?
I tried it and it failed. You can see the error log here: https://gist.github.com/ImanHosseini/8b696ea81946f6248c73652aa9eaeca0
This is just a fresh hello world CMake project, nothing tweaked/tampered with.
Your search path is incorrect.VS 2019 16.2 has clang in the folder: “C:\Program Files (x86)\Microsoft Visual Studio\2019\<Edition>\VC\Tools\Llvm\8.0.0\bin”
VS 2019 16.3 will have clang in the folder: “C:\Program Files (x86)\Microsoft Visual Studio\2019\<Edition>VC\Tools\Llvm\bin”
It appears that <filesystem> cannot be included (at least using clang-cl.exe), there is a weird, relatively unmentioned error:
EXEC : error : cannot mangle this built-in char8_t type yet
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