c – How do I tell CMAKE Use Clang on Windows?

I have a C project built using CMake. I usually build on OSX, but now I am trying to use the Windows version. For compatibility reasons, I want to use Clang on Windows .

I installed the pre-compiled Clang 3.8 binaries 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:

>When I call cmake –build, how do I tell CMake to use clang?
>How to check before building the compiler configured by CMake?

In addition to the Clang compiler itself, you also need a build/link environment for Windows

The latest CMake 3.6 version has several integrated support Clang build environments on Windows (e.g. Visual Studio, Cygwin; see Release Notes).

I just used A successful test

> LLVM-3.9.0-r273898-win32.exe from http://llvm.org/builds/
> cmake-3.6.0-rc4-win64-x64. msi is installed from https://cmake.org/download/
> Microsoft VS2015 Community Edition version 14.0.23107.0

All are installed to its standard path, and its bin directory is located in the global PATH environment.

The part you need to know is to use the CMake -T “LLVM-vs2014” command line option to set the correct toolset. During the configuration process, CMake will tell you which compiler it found/adopted.

< p>CMakeLists.txt

cmake_minimum_required(VERSION 3.6)

project(HelloWorld)

file(
WRITE main.cpp
"#include \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 tips

Please note Meaning, I have added LLVM to the search path during the setup process:

LLVM Installation with Add to PATH

You can cross check the available “platform toolset” in the property page of any VS project:

VS Project Properties-Platform Toolsets

Reference

> What is the -D define to tell Cmake where to find nmake?
> Linker for Clang?
> Switching between GCC and Clang/LLVM using CMake

I have a C project built using CMake. Usually built on OSX, but now I am trying to use the Windows version. For compatibility reasons, I want to use Clang on Windows.

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

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:

>When I call cmake -build, how do I tell CMake to use clang?
>How to check before building the compiler configured by CMake?

In addition to the Clang compiler itself, you also need a build/link environment for Windows.

Latest The CMake 3.6 version has several integrated support Clang build environments on Windows (e.g. Visual Studio, Cygwin; see Release Notes).

I just used a successful test

> 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

Install all to its standard path, and its bin directory is in the global PATH environment.

The part you need to know is to use CMake -T “LLVM-vs2014” command line option sets the correct toolset. During the configuration process, CMake will tell you the compiler it found/adopted.

CMakeLists.txt

cmake_minimum_required(VERSION 3.6)

project(HelloWorld)

file(
WRITE main.cpp
"#include \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 CX X 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!< /pre>

Installation tips

Please note that I have added LLVM to the search path during the setup process:

LLVM Installation with Add to PATH

You can cross check the available "platform toolset" in the property page of any VS project:

VS Project Properties-Platform Toolsets

Reference

> What is the -D define to tell Cmake where to find nmake?
> Linker for Clang?
> Switching between GCC and Clang/LLVM using CMake

Leave a Comment

Your email address will not be published.