2

When I edit the CMakeLists.txt file, the first time it works and uses the cache, but the second time I always get

1> The existing cache contains references to non-existing MSVC compiler. Deleting cache and regenerating.

This only happens If I use the clang compiler. If I use the default msvc compiler the cache is always used

// main.cpp

int main()
{
    return 0;
}

#CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
add_executable(app main.cpp)

//CMakeSettings.json
{
  // See https://go.microsoft.com//fwlink//?linkid=834763 for more information about this file.
  "configurations": [
    {
      "name": "x86-Debug",
      "generator": "Ninja",
      "configurationType": "Debug",
      "inheritEnvironments": [ "msvc_x86" ],
      "buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
      "installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
      "cmakeCommandArgs": "",
      "buildCommandArgs": "-v",
      "ctestCommandArgs": "",
      "variables": [
        {
          "name": "CMAKE_CXX_COMPILER",
          "value": "clang-cl"
        },
        {
          "name": "CMAKE_C_COMPILER",
          "value": "clang-cl"
        },
        {
          "name": "CMAKE_SYSTEM_NAME",
          "value": "Windows"
        }
      ]
    }
  ]
}
Gam
  • 1,254
  • 1
  • 9
  • 18
  • Which versions of CLang do you have installed? The package that is part of Visual Studio 2017? What does `clang --version` output? – Florian Dec 10 '17 at 19:41
  • @Florian clang version 5.0.0 (tags/RELEASE_500/final) Target: x86_64-pc-windows-msvc – Gam Dec 10 '17 at 23:00

2 Answers2

2

Turning my comments into an answer

You have to give full paths for your compilers when you use with :

CMakeSettings.json

...
"variables": [
     {
         "name": "CMAKE_CXX_COMPILER",
         "value": "C:\\Program Files\\LLVM\\bin\\clang-cl.exe"
     },
     {
         "name": "CMAKE_C_COMPILER",
         "value": "C:\\Program Files\\LLVM\\bin\\clang-cl.exe"
     },
     {
         "name": "CMAKE_SYSTEM_NAME",
         "value": "Windows"
     }
]
...

Here is what I think is happening:

  • CMake always translates compiler paths to absolute paths (your first call)
  • In the second call VS2017 does again override the CMAKE_CXX_COMPILER variables to CMake
    • Which is not intended since it overwrites/forces the values again, but CMake does remember that it already searched for the compiler
    • Et voila, you get an error message about a changed compiler

I think this being a bug in VS2017's usage of CMake.

References

Florian
  • 39,996
  • 9
  • 133
  • 149
  • @James Have to checkout the video later. Just to for clarification: Do you have CLang installed/visible in the `PATH`? Can you try to use full paths to the compiler in the compiler settings? Can you check what you have as `CMAKE_CXX_COMPILER` in your `CMakeCache.txt`? – Florian Dec 12 '17 at 13:19
  • Using the full path has solved by problem, thanks very much!!!!. I have Clang visible in my PATH, I thought that was the right thing to do. For the record, CMake was detecting the correct path to clang-cl. As I said, this is a fresh windows 10 installation, and I've only installed Visual Studio 2017 15.5 + LLVM 5.0. – Gam Dec 12 '17 at 13:36
  • Please edit your answer regarding using the full path so that I can accept it. – Gam Dec 12 '17 at 13:44
  • @James You're welcome. And, sure I'm going to update my answer later. The problem is that I now understand what's happening and I think this sort of a bug in VS2017 usage of CMake. I've to still do some testing, but here is what I think is happening: CMake always translates compiler paths to absolute paths (your first call). In the second call VS2017 does again use the `-D` command line calls to CMake (which is not intended since it overwrites/forces the values again) and CMake does remember that it already searched for the compiler. Et voila, you get an error message about a changed compiler. – Florian Dec 12 '17 at 13:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/161066/discussion-between-james-and-florian). – Gam Dec 12 '17 at 21:56
0

I get the same issue with the msvc compiler in VS 2022 17.2.4, where every other time I save the CMakeLists.txt file in the editor (without changing anything), it regenerates the cmake cache. At the top of the CMake output, it says:

The existing cache contains references to non-existing MSVC compiler.

I found that if I remove the "cacheVariables" definition in the "windows-base" preset of the VS generated, CMakePresets.json file, i.e.

   "cacheVariables": {
      "CMAKE_C_COMPILER": "cl.exe",
      "CMAKE_CXX_COMPILER": "cl.exe"
   }

...it fixes the problem (and builds without issue).

Update

After updating to VS 2022 17.4.1, the issue no longer occurs.

jspicer
  • 1
  • 1