Issue
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"
}
]
}
]
}
Solution
Turning my comments into an answer
You have to give full paths for your compilers when you use ninja with visual-studio-2017:
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
- How do I tell CMake to use Clang on Windows?
- Building a x86 application with CMake, Ninja and Clang on x64 Windows
- Visual C++ Team Blog: Customizing your Environment with Visual C++ and Open Folder
Answered By - Florian Answer Checked By - Pedro (WPSolving Volunteer)