3

On Windows 10 with VS2017 and LLVM installed

and CMAKE just takes VC compiler as default and ignores both set command in the CMakeLists.txt and command line -DCMAKE_CXX_COMPILER

I have no idea how to make it use LLVM Clang

I checked out many existing questions but they do not work for me

CMakeLists.txt

cmake_minimum_required(VERSION 3.11)
set(CMAKE_CXX_COMPILER "C:\\Program Files\\LLVM\\bin\\Clang++")
project(CMakeTest1 VERSION 0.0.0 LANGUAGES CXX)
aux_source_directory(. MAINDIR)
add_executable(CMakeTest1 ${MAINDIR})

main.cpp

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello World!" << endl;
    return 0;
}

running command with git bash

***@DESKTOP-8KJ4J0H MINGW64 /d/CMakeTest1
$ cmake -DCMAKE_CXX_COMPILER="C:\Program Files\LLVM\bin\Clang++" .
-- Building for: Visual Studio 15 2017
-- The CXX compiler identification is MSVC 19.13.26129.0
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.13.26128/bin/Hostx86/x86/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.13.26128/bin/Hostx86/x86/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: D:/CMakeTest1

my CXX compiler identification is forced to be MSVC and cannot be changed by any method.

no idea why

EDITED: what i wanna strongly declare is, i swear and clearly know that i CLEANED ALL GENERATED FILES form the folder and there are ONLY JUST CMakeLists.txt and main.cpp existing. and I DON'T WANT TO BE ASSUMED having old cache files unremoved

Alsein
  • 4,268
  • 1
  • 15
  • 35
  • 1
    What is the content of `CMakeCache.txt`? Have you try to remove this file before run cmake? – hgminh Mar 28 '18 at 10:15
  • As explained in [the documentation](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER.html#variable:CMAKE_%3CLANG%3E_COMPILER), once set it can't be changed. You must remove *all* CMake generated project files and remake it from fresh with `CMAKE_CXX_COMPILER` set to the compiler you want to use. – Some programmer dude Mar 28 '18 at 10:15
  • 2
    On a slightly related note, it's not recommended to create the CMake build/project files in the same directory as the rest of your source tree. Create a sub-directory (e.g. `mkdir build`), go to that directory (`cd build`) and generate the build files there (`cmake ..`). This will make it *much* easier to regenerate your project from scratch, as needed when changing compiler, since you just remove the separate build directory. – Some programmer dude Mar 28 '18 at 10:18
  • what i wanna strongly declare is, i swear and clearly know that i cleaned all generated files form the folder and there are only just CMakeLists.txt and main.cpp existing @hgminh – Alsein Mar 28 '18 at 10:22
  • Works well for me using Cmake v3.10 on linux, Clang is used. Can you use CMAKE_EXPORT_COMPILE_COMMANDS to ensure clang is not used ? – Charles Gueunet Mar 28 '18 at 10:42
  • I tried using cmake in a build folder and that causes `cmake --build ..` fail with `Error: could not load cache` – Alsein Mar 28 '18 at 10:43
  • `CMake Warning: Manually-specified variables were not used by the project: CMAKE_EXPORT_COMPILE_COMMANDS -- Build files have been written to: D:/CMakeTest1` @Charles Gueunet – Alsein Mar 28 '18 at 10:46
  • @Alsein You need to use -DCMAKE_EXPORT_COMPILE_COMMANDS=ON to use it, as -D is used to set a variable on command line. PS: Why do you use --build with cmake ? – Charles Gueunet Mar 28 '18 at 10:50
  • yes im sure i used -DCMAKE_EXPORT_COMPILE_COMMANDS=ON @Charles Gueunet – Alsein Mar 28 '18 at 11:00
  • btw is there any disadvantage using --build?@Charles Gueunet – Alsein Mar 28 '18 at 11:00
  • You can't declare/change the compiler for Visual Studio Solutions generator via `CMAKE_CXX_COMPILER`. Changing the compiler in VS works by defining the toolset you want to use. Use `cmake -T"LLVM-vs2014"`. – Florian Mar 28 '18 at 11:06
  • @Alsein Export compile command is only supported by Makefile/Ninja generator and as told by hgminh it seems you are not using these. That is why you don't have this command enable. For --build, I seen no disadvantages, I was just curious. – Charles Gueunet Mar 28 '18 at 11:07
  • i dont wanna use anything from VS so that it would work not only on windows @Florian – Alsein Mar 28 '18 at 11:19
  • so is there any way to make it work with LLVM Clang?@Charles Gueunet – Alsein Mar 28 '18 at 11:20
  • @Alsein if you use cmake-gui, it is easier to change the generator used. Can you try it ? – Charles Gueunet Mar 29 '18 at 12:01

2 Answers2

1

This may be due to default generator of CMake in Windows.

-- Building for: Visual Studio 15 2017
...

You can change the generator by adding -G <generator name> to the command line. In your case, I think -G 'MSYS Makefiles' may work.

hgminh
  • 1,178
  • 1
  • 8
  • 26
  • `CMake Error: CMake was unable to find a build program corresponding to "MinGW Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.` – Alsein Mar 28 '18 at 11:03
  • sorry, I copy the wrong one, try `-G 'MSYS Makefiles'` instead. I think it should work on git bash – hgminh Mar 28 '18 at 11:04
  • `CMake Error: CMake was unable to find a build program corresponding to "MSYS Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.` – Alsein Mar 28 '18 at 11:16
1

This happens because CMAKE_CXX_COMPILER was inferred from your generator (The latest Visual Studio is used by default on windows), so it is cl.exe.

In order to switch to clang you should use other generator e.g. Ninja or Unix Makefiles.

If you want to use VS with clang-cl.exe, you need to set platform toolset to appropriate version by adding flag -TLLVM-vs2014.

See this instructions for more info.

ivaigult
  • 6,198
  • 5
  • 38
  • 66