CMAKE_SYSTEM_VERSION - MSVC - First try the environment variable WindowsSdkVersion
On Windows, it's common to run 'vcvarsall.bat' to setup the development environment, especially if you have multiple versions of MS Visual Studio installed. This sets not only the path to the executables, but also to the Windows SDK. The Windows SDK version is set in the environment variable: WindowsSDKVersion.
The problem is that CMake, by default, may select an SDK different from that set in the WindowsSDKVersion variable.
I'm not sure how CMake calculates which SDK version it should use when compiling natively and not cross-compiling, but it would be wonderful if it would first check Window's standard environment variables.
For example, my system presently has 4 versions Windows 10 SDK Kits (10.0.10240.0, 10.0.16299.0, 10.0.17125.0, and 10.0.17134.0). Running 'vcvarsall.bat' sets WindowsSDKVersion to the latest compatible SDK: '10.0.17134.0', but CMake creates a MSVS project using 10.0.16299.0.
-- As a work-around, I can prepend this to the CMakeLists files:
set(CMAKE_SYSTEM_VERSION_TEMP $ENV{WindowsSDKVersion})
string(REGEX REPLACE "([0-9.]+).*" "\\1" CMAKE_SYSTEM_VERSION_TEMP "${CMAKE_SYSTEM_VERSION_TEMP}" )
if(CMAKE_SYSTEM_VERSION_TEMP)
set(CMAKE_SYSTEM_VERSION ${CMAKE_SYSTEM_VERSION_TEMP} CACHE INTERNAL "")
endif()
But, this shouldn't be necessary.