How do I tell CMake to use Clang on Windows?

55,764

Solution 1

You also need - in addition to the Clang compilers itself - an build/link environment for Windows.

The latest CMake 3.6 builds do have several integrated supported Clang build environments on Windows (e.g. Visual Studio, Cygwin; see Release Notes).

I've just run a successful test with

All installed to their standard paths with their bin directories in the global PATH environment.

The part you need to know is setting the right toolset with the CMake -T"LLVM-vs2014" command line option. During the configuration process CMake will let you know which compiler it has found/taken.

CMakeLists.txt

cmake_minimum_required(VERSION 3.6)

project(HelloWorld)

file(
    WRITE main.cpp 
        "#include <iostream>\n"
        "int main() { std::cout << \"Hello World!\" << std::endl; return 0; }"
)
add_executable(${PROJECT_NAME} main.cpp)

Windows Console

...> mkdir VS2015
...> cd VS2015
...\VS2015> cmake -G"Visual Studio 14 2015" -T"LLVM-vs2014" ..
-- The C compiler identification is Clang 3.9.0
-- The CXX compiler identification is Clang 3.9.0
-- Check for working C compiler: C:/Program Files (x86)/LLVM/msbuild-bin/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/LLVM/msbuild-bin/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/LLVM/msbuild-bin/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/LLVM/msbuild-bin/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: .../VS2015
...\VS2015> cmake --build . 
Microsoft (R)-Buildmodul, Version 14.0.23107.0
[...]
...\VS2015> Debug\HelloWorld.exe
Hello World!

Installation Hints

Please note that I have added LLVM to my search paths during setup:

LLVM Installation with Add to PATH

And you can crosscheck the available "Platform Toolsets" in any VS project's property page:

VS Project Properties - Platform Toolsets

References

Solution 2

As an update for Visual Studio 2019, you can install the clang-cl toolchain via the Visual Studio Installer and use this to generate a .sln file:

> mkdir build && cd build
> cmake .. -G "Visual Studio 16 2019" -T ClangCL -A x64

Of course you can also just open the folder containing the CMakeLists.txt file now, and VS will provide decent support, allowing you to select the compiler toolchain, however it won't let you use the graphics debugger which might be important to you.

Solution 3

Follow these instructions:

Install choco if you don't have it: https://chocolatey.org/install

choco install ninja -y
choco install cmake -y
choco install llvm -y

Reset your shell so environment variables are set properly (you can check if bin folders for each are added to your Path).

Using Ninja

From PowerShell

$env:CC="C:\Program Files\LLVM\bin\clang.exe"
$env:CXX="C:\Program Files\LLVM\bin\clang++.exe"
cmake -S ./ -B ./build -G "Ninja-Multi-Config"
cmake --build ./build --config Release

From GUI

Go to your project and run:

cmake-gui .

From the upper menu select Tools/Configure and follow these settings:

Choose "Ninja Multi-Config" and Specify native compilers:

ninja

Give the path to the compilers: compilers

Finally, run

cmake --build ./build --config Release

Using Visual Studio

Using GUI

In some folder install llvm-utils:

git clone https://github.com/zufuliu/llvm-utils.git
cd llvm-utils/VS2017
.\install.bat

Go to your project and run:

cmake-gui .

From the upper menu select Tools/Configure and follow these settings:

Choose Visual Studio 2019 and 2nd option (specify native compilers). Set LLVM_v142 for Visual Studio 2019 and above. See here for older versions for others. visual_studio

Give the path to the compilers: compilers

Finally, run

cmake --build ./build --config Release
Share:
55,764
sdgfsdh
Author by

sdgfsdh

fun times

Updated on January 18, 2022

Comments

  • sdgfsdh
    sdgfsdh over 2 years

    I have a C++ project that builds using CMake. I usually build on OSX but now I am trying to get a Windows version working too. I would like to use Clang on Windows for compatibility reasons.

    I installed the pre-compiled Clang 3.8 binary from LLVM:

    C:\Program Files\LLVM\bin\clang.exe
    C:\Program Files\LLVM\bin\clang++.exe
    

    It is also installed on my PATH:

    >clang++
    clang++.exe: error: no input files
    

    I have two questions:

    1. How do I tell CMake to use clang++ when I call cmake --build?
    2. How can I check before building which compiler CMake is configured with?
  • sdgfsdh
    sdgfsdh almost 8 years
    Thank you! The bit I was missing was -T"LLVM-vs2014"
  • kchoi
    kchoi over 7 years
    awfully confusing. CMake should consider changing -G to MSBuild because that's what it's outputing, and suboptions to set the actual compiler toolchain
  • Florian Wolters
    Florian Wolters over 7 years
    Weird. CMake still chooses the default cl.exe on my machine when doing this with Microsoft Visual Studio 2015, CMake 3.7.1 and LLVM/Clang 3.9.1: The CXX compiler identification is MSVC 19.0.24215.1.
  • Florian
    Florian over 7 years
    @FlorianWolters The difference could be that I have clang in my search paths. If you call clang directly from your command line, does it find the compiler and is showing clang.exe: error: no input files?
  • Florian Wolters
    Florian Wolters over 7 years
    @Florian: clang is in the PATH. I have been able to narrow the problem: I haven't installed the LLVM-3.9.1-win32.exe, but extracted it via 7-Zip and executed tools\msbuild\install.bat manually. That does not work and the default MSVC compiler clis used. When installing it, it works as described. Now I have to find out, why it does not work for my portable solution.
  • Florian Wolters
    Florian Wolters over 7 years
    (Almost)portable solution if not running the LLVM installer: Replace the string <LLVMInstallDir>$(Registry:HKEY_LOCAL_MACHINE\SOFTWARE\LLVM\‌​LLVM)</LLVMInstallDi‌​r> in all .props files below the dir tools\msbuild with the string <LLVMInstallDir>$(LLVM_ROOT)</LLVMInstallDir> and make sure the env variable LLVM_ROOT points to the dir of the "portable" installation, e.g. C:\llvm-3.9.1-x86. Problem: Admin privileges are required to "install" the LLVM toolsets for MSBuild. If someone knows how-to use toolsets with MSBuild that are not stored in the default dir, please let me know.
  • yau
    yau about 7 years
    If someone struggles using the CMake-GUI app, just pass LLVM-vs2014 in the input field for the toolset (-T parameter) there, and not -T"LLVM-vs2014".
  • PaulR
    PaulR almost 7 years
    A hint for those using the LLVM installer: install Visual Studio before LLVM. The LLVM installer only installs the toolset for already installed Visual Studio versions (for obvious reasons). Otherwise reinstalling LLVM after installing a new version of Visual Studio helps.
  • Andreas Duering
    Andreas Duering over 6 years
    Future readers, beware. Using CMake 3.9.1 and clang 4.0.1, CMAKE_C_FLAGS gets set to /DWIN32 -fms-extensions -fms-compatibility -D_WINDOWS -Wall - and clang-cl complains about the unknown compiler flags. -- but it works fine with the mentioned cmake release.
  • Alex G. G.
    Alex G. G. over 2 years
    Thank you for the command, just wanted to inform that now you can use the debugger too.
  • trojanfoe
    trojanfoe over 2 years
    @AlexG.G. The Visual Studio Graphics Debugger?
  • Alex G. G.
    Alex G. G. over 2 years
    Nvm I had understood the ui to debug but you mean the tools to debug graphics right?