2

I found someone mentioned to use such command line to use cmake with vs2019 and llvm toolset:

cmake -G "Visual Studio 2019" -T ClangCL

However, this would use the llvm version, which is 11.0 for now, was installed by the visual studio instanller.

I want to use a new version, thus 12.0, of llvm which I already installed somewhere else but I don't know how to make cmake and vs to use that.


UPDATE:

What I tried to set the compiler via command line:

E:\my_proj>cmake -G "Visual Studio 16 2019" -A x64 -T "ClangCL" -DCMAKE_C_COMPILER="C:\Program Files\LLVM\bin\clang.exe" -DCMAKE_CXX_COMPILER="C:\Program Files\LLVM\bin\clang++.exe" .
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.19042.
-- The C compiler identification is Clang 11.0.0 with MSVC-like command-line
-- The CXX compiler identification is Clang 11.0.0 with MSVC-like command-line
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/Llvm/x64/bin/clang-cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/Llvm/x64/bin/clang-cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done

As you could see the cmake picked the clang from the vs installed location and ignored the command line.

jayatubi
  • 1,972
  • 1
  • 21
  • 51
  • What have you tried? Does setting `CMAKE_CXX_COMPILER` not work? – Alex Reinking Aug 20 '21 at 08:20
  • I tried ```set (CMAKE_C_COMPILER "C:/Program Files/LLVM/bin/clang.exe") set (CMAKE_CXX_COMPILER "C:/Program Files/LLVM/bin/clang++.exe")``` in the beginning of my CMakeLists.txt but that seems to be ignored. – jayatubi Aug 20 '21 at 08:29
  • @jayatubi: Have you tried **other approaches**, described in the answers to [that question](https://stackoverflow.com/questions/45933732/how-to-specify-a-compiler-in-cmake)? E.g. set `CC` and `CXX` environment variable, or pass `-D CMAKE_C_COMPILER` and `-D CMAKE_CXX_COMPILER` options to `cmake`? – Tsyvarev Aug 20 '21 at 11:48
  • @Tsyvarev No. It always use the clang which was installed inside visual studio regardless what I set in the command line. – jayatubi Aug 20 '21 at 12:03
  • can you show us the output of "C:\Program Files\LLVM\bin\clang.exe" --version – harry Aug 31 '21 at 11:39
  • @harry It's `12.0.1`. I just download it from llvm.org and fresh installed. – jayatubi Sep 01 '21 at 03:08
  • https://discourse.cmake.org/t/overriding-cmake-cxx-compiler-with-vs2017-on-windows-10/1311/2 hope this clears your question. BTW, I tried to install clang with VS installer and it installed 12.0.0. – harry Sep 01 '21 at 05:20
  • @harry So does that mean with cmake the user could only select the toolset which provides by vs and can't use any external compilers? – jayatubi Sep 01 '21 at 06:48
  • yes, that's right. – harry Sep 01 '21 at 06:49
  • Thanks for the comment. Could you add a detailed answer then I will mark it. – jayatubi Sep 01 '21 at 06:57

1 Answers1

1

As described below, setting CMAKE flags explicitly has no effect when using VS generator. Thus choosing the default compiler installed by Visual Studio installer.

The VS generator produces .vcxproj files and uses MSBuild to actually drive the build. The values of CMAKE_{C,CXX}_COMPILER are computed automatically from the compiler MSBuild chooses so setting them has no effect.

harry
  • 970
  • 6
  • 25