|
| 1 | +--- |
| 2 | +title: "Clang/LLVM support in Visual Studio CMake projects" |
| 3 | +ms.date: "07/01/2019" |
| 4 | +ms.description: "Configure a CMake project in Visual Studio to use the Clang/LLVM toolchain." |
| 5 | +helpviewer_keywords: ["Clang support for C++"] |
| 6 | +--- |
| 7 | + |
| 8 | +# Clang/LLVM support in Visual Studio CMake projects |
| 9 | + |
| 10 | +::: moniker range="<=vs-2017" |
| 11 | + |
| 12 | +Clang support is available in Visual Studio 2019. |
| 13 | + |
| 14 | +::: moniker-end |
| 15 | + |
| 16 | +::: moniker range="vs-2019" |
| 17 | + |
| 18 | +You can use Visual Studio with Clang to edit and debug C++ CMake projects that target Windows or Linux. |
| 19 | + |
| 20 | +**Windows**: Visual Studio 2019 version 16.1 includes support for editing, building, and debugging with Clang/LLVM in CMake projects targeting Windows. |
| 21 | + |
| 22 | +**Linux**: For Linux CMake projects, no special Visual Studio support is required. You can install Clang using your distro's package manager, and add the appropriate commands in the CMakeLists.txt file. |
| 23 | + |
| 24 | +## Install |
| 25 | + |
| 26 | +For best IDE support in Visual Studio, we recommend using the latest Clang compiler tools for Windows. If you don't already have those, you can install them by opening the Visual Studio Installer and choosing **Clang compiler for Windows** under **Desktop development with C++** optional components. |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +## Create a new configuration |
| 31 | + |
| 32 | +To add a new Clang configuration to a CMake project: |
| 33 | + |
| 34 | +1. Right-click on CMakeLists.txt in **Solution Explorer** and choose **CMake settings for project**. |
| 35 | + |
| 36 | +1. Under **Configurations**, press the **Add Configuration** button: |
| 37 | + |
| 38 | +  |
| 39 | + |
| 40 | +1. Choose the desired Clang configuration (note that separate Clang configurations are provided for Windows and Linux), then press **Select**: |
| 41 | + |
| 42 | +  |
| 43 | + |
| 44 | +1. To make modifications to this configuration, use the **CMake Settings Editor**. For more information, see [Customize CMake build settings in Visual Studio](customize-cmake-settings.md). |
| 45 | + |
| 46 | +## Modify an existing configuration to use Clang |
| 47 | + |
| 48 | +To modify an existing configuration to use Clang, follow these steps: |
| 49 | + |
| 50 | +1. Right-click on CMakeLists.txt in **Solution Explorer** and choose **CMake settings for project**. |
| 51 | + |
| 52 | +1. Under **General** select the **Toolset** dropdown and choose the desired Clang toolset: |
| 53 | + |
| 54 | +  |
| 55 | + |
| 56 | +## Custom Clang locations |
| 57 | + |
| 58 | +By default, Visual Studio looks for Clang in two places: |
| 59 | + |
| 60 | +- (Windows) The internally installed copy of Clang/LLVM that comes with the Visual Studio installer. |
| 61 | +- (Windows and Linux) The PATH environment variable. |
| 62 | + |
| 63 | +You can specify another location by setting the **CMAKE_C_COMPILER** and **CMAKE_CXX_COMPILER** CMake variables in **CMake Settings**: |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +## Clang compatibility modes |
| 68 | + |
| 69 | +For Windows configurations, CMake by default invokes Clang in [clang-cl](https://llvm.org/devmtg/2014-04/PDFs/Talks/clang-cl.pdf) mode and links with the Microsoft implementation of the Standard Library. By default, **clang-cl.exe** is located in `C:\Program Files (x86)\Microsoft Visual Studio\2019\Common7\IDE\CommonExtensions\Microsoft\Llvm\bin`. |
| 70 | + |
| 71 | + You can modify these values in **CMake Settings** under **CMake variables and cache**. Click **Show advanced variables**. Scroll down to find **CMAKE_CXX_COMPILER**, then click the **Browse** button to specify a different compiler path. |
| 72 | + |
| 73 | +## Edit, build, and debug |
| 74 | + |
| 75 | +After you have set up a Clang configuration, you can build and debug the project. Visual Studio detects that you are using the Clang compiler and provides IntelliSense, highlighting, navigation, and other editing features. Errors and warnings are displayed in the **Output Window**. |
| 76 | + |
| 77 | +When debugging, you can use breakpoints, memory and data visualization, and most other debugging features. Some compiler-dependent features such as Edit and Continue are not available for Clang configurations. |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +::: moniker-end |
0 commit comments