47

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
  • 33,689
  • 26
  • 132
  • 245
  • There's the `toolchain.txt` file, lookup for this one in the docs. – πάντα ῥεῖ Jul 03 '16 at 16:20
  • On ubuntu, I use CC='g++ -m64' when I invoke the g++ compiler via Make (Make has $(CC) in it) I use CC="clang++ -m64' to invoke clang++. (I do not use CMake nor Windows) – 2785528 Jul 03 '16 at 17:35
  • What's wrong with using `-DCMAKE_CXX_COMPILER=C:/path/to/clang++`? – usr1234567 Jul 03 '16 at 19:14
  • 1
    @usr1234567 It's platform-dependent, i.e. can't use a single, portable recipe like `cmake .. && cmake --build .`, but have to except for windos. – K3---rnc Oct 19 '17 at 15:25

3 Answers3

45

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

Community
  • 1
  • 1
Florian
  • 39,996
  • 9
  • 133
  • 149
  • Thank you! The bit I was missing was `-T"LLVM-vs2014"` – sdgfsdh Jul 04 '16 at 10:00
  • 1
    awfully confusing. CMake should consider changing -G to MSBuild because that's what it's outputing, and suboptions to set the actual compiler toolchain – kchoi Oct 04 '16 at 21:49
  • 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 Wolters Jan 11 '17 at 19:35
  • @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 Jan 11 '17 at 19:59
  • @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 `cl`is 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 Jan 11 '17 at 20:18
  • 1
    (Almost)portable solution if not running the LLVM installer: Replace the string `$(Registry:HKEY_LOCAL_MACHINE\SOFTWARE\LLVM\LLVM)` in all `.props` files below the dir `tools\msbuild` with the string `$(LLVM_ROOT)` 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. – Florian Wolters Jan 11 '17 at 21:49
  • 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"`. – yau Mar 03 '17 at 22:46
  • 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. – PaulR Jul 18 '17 at 11:14
  • 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. – Andreas Duering Aug 29 '17 at 12:36
22

As an update for Visual Studio 2019 and 2022, 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

or

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

However a much better workflow is to simply open the folder containing the CMakeLists.txt file directly in VS and it will recognise it as a CMake project and provide the ability to set the compiler etc. directly.

The only drawback I could find to this approach is that the Visual Studio Graphics Debugger workflow doesn't work with it, which will affect you if you are developing with DirectX etc.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • Thank you for the command, just wanted to inform that now you can use the debugger too. – Alex G. G. Nov 29 '21 at 07:24
  • @AlexG.G. The Visual Studio Graphics Debugger? – trojanfoe Nov 29 '21 at 08:45
  • Nvm I had understood the ui to debug but you mean the tools to debug graphics right? – Alex G. G. Nov 29 '21 at 09:51
  • 2
    In VS 2022 I'm getting a CMake error: `The CXX compiler identification is unknown CMake Error at CMakeLists.txt:1 (project): No CMAKE_C_COMPILER could be found.` when using `ClangCL` toolset. Any idea what could be wrong? I have installed Clang tools via VS installer. – szx Feb 07 '23 at 15:01
  • @szx I'm not certain, no. What happens if you open the folder in VS2022 and then play with the cmake settings to ensure the correct compiler is being used? – trojanfoe Feb 08 '23 at 09:41
11

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
Amin Ya
  • 1,515
  • 1
  • 19
  • 30