How do I tell CMake to use Clang on Windows?
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
- LLVM-3.9.0-r273898-win32.exe from http://llvm.org/builds/
- cmake-3.6.0-rc4-win64-x64.msi from https://cmake.org/download/
- Microsoft VS2015 Community Edition Version 14.0.23107.0
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.exeHello World!
Installation Hints
Please note that I have added LLVM to my search paths during setup:
And you can crosscheck the available "Platform Toolsets" in any VS project's property page:
References
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.
Follow these instructions:
Install choco if you don't have it: https://chocolatey.org/install
Use A
in the prompts.
choco install mingwchoco install llvmchoco install cmake
Reset your shell so environment variables are set properly (you can check if bin folders for each are added to your Path).
Using MinGW
Go to your project and run:
cmake-gui .
From the upper menu select Tools/Configure
and follow these settings:
Choose MinGW Makefiles and 2nd option (specify native compilers):
Give the path to the compilers:
Using Visual Studio
In some folder install llvm-utils:
git clone https://github.com/zufuliu/llvm-utils.gitcd 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. See here for others.