简体   繁体   English

如何设置默认的 Windows 工具包 (SDK) 版本?

[英]How to set the default Windows kit (SDK) version?

I used to use Windows 8.1 SDK for my C++ application, and everything's working fine.我曾经为我的 C++ 应用程序使用 Windows 8.1 SDK,一切正常。 Today I installed the Windows 10 SDK and I can't find a way to make it the default one.今天我安装了 Windows 10 SDK,但找不到将其设置为默认 SDK 的方法。

I can hard-code the new SDK path in the Visual Studio project settings, but that is highly undesirable.我可以在 Visual Studio 项目设置中对新的 SDK 路径进行硬编码,但这是非常不可取的。 I want the new kit to be used by default for every new project.我希望每个新项目默认使用新工具包。 There's no environment variable for the SDK, and I can't find anything in the registry, either. SDK 没有环境变量,我在注册表中也找不到任何内容。
More precisely, there are Windows SDK entries in the registry, but what I need - the C++ includes and libraries - is called the Windows Kit (located in C:\Program Files (x86)\Windows Kits).更准确地说,注册表中有 Windows SDK 条目,但我需要的 - C++ 包含和库 - 称为 Windows工具包(位于 C:\Program Files (x86)\Windows Kits)。

Ok, it seems not a lot of people face thist problem, but i'll still post a workaround to set default sdk version.好吧,似乎没有多少人遇到这个问题,但我仍然会发布一个解决方法来设置默认的 sdk 版本。 It worked with Visual Studio 2017 Communtity.它适用于 Visual Studio 2017 社区。 Consider the following situation:考虑以下情况:

  • You've got a solution with projects you must not retarget你有一个项目的解决方案,你不能重新定位

  • You had older VS installed earlier on your PC您之前在 PC 上安装了较旧的 VS

  • When you open the solution, VS sets SDK version as 8.1 for some reason, while you use Windows SDK 10当您打开解决方案时,VS 出于某种原因将 SDK 版本设置为 8.1,而您使用的是 Windows SDK 10

  • When you try to build you have the following:当您尝试构建时,您会遇到以下情况:

Error MSB8036 The Windows SDK version 8.1 was not found.错误 MSB8036 找不到 Windows SDK 版本 8.1。 Install the required version of Windows SDK or change the SDK version in the project property pages or by right-clicking the solution and selecting "Retarget solution".安装所需版本的 Windows SDK 或在项目属性页中更改 SDK 版本,或右键单击解决方案并选择“重新定位解决方案”。

because SDK 8.1 is not installed properly on your PC, and reinstalling it somewhy does not solve the problem.因为 SDK 8.1 没有在您的 PC 上正确安装,并且以某种方式重新安装它并不能解决问题。

  • Others on your team don't have such a problem as the projects don't have Windows SDK version explicitly defined inside *.vcxproj files.您团队中的其他人没有这样的问题,因为项目没有在 *.vcxproj 文件中明确定义的 Windows SDK 版本。

So, obviusly, MS build system with multiple SDKs installed has some kind of confusion, and the worst part is that it results in defining corruptly installed SDK as your default.因此,很明显,安装了多个 SDK 的 MS 构建系统存在某种混乱,最糟糕的部分是它导致将损坏安装的 SDK 定义为默认值。 I used the following workaround to set default windows SDK explicitly:我使用以下解决方法来显式设置默认 Windows SDK:

  • Go to [VS installment path]\Common7\IDE\VC\VCTargets ,example:进入【VS安装路径】\Common7\IDE\VC\VCTargets ,例如:

C:\Program Files (x86)\Microsoft Visual Studio\2017\WDExpress\Common7\IDE\VC\VCTargets\Microsoft.Cpp.WindowsSDK.props) C:\Program Files (x86)\Microsoft Visual Studio\2017\WDExpress\Common7\IDE\VC\VCTargets\Microsoft.Cpp.WindowsSDK.props)

  • Open file Microsoft.Cpp.WindowsSDK.props as an administrator以管理员身份打开文件Microsoft.Cpp.WindowsSDK.props
  • Find line查找行
    <DefaultWindowsSDKVersion Condition="'$(DefaultWindowsSDKVersion)' == ''
    and '$(AppContainerApplication)' != 'true'">8.1</DefaultWindowsSDKVersion>

change 8.1 to the SDK version you're sure you have properly installed, then save the file.将 8.1 更改为您确定已正确安装的 SDK 版本,然后保存文件。 In my case it was 10.0.17763.0, so the final line was在我的例子中是 10.0.17763.0,所以最后一行是

    <DefaultWindowsSDKVersion Condition="'$(DefaultWindowsSDKVersion)' == ''
    and '$(AppContainerApplication)' != 'true'">10.0.17763.0</DefaultWindowsSDKVersion>

Now reopen your solution and try to build it.现在重新打开您的解决方案并尝试构建它。 Should work fine.应该工作正常。

My first troubleshooting tips would be to uninstall all the Visual Studio stuff you have, then reboot.我的第一个故障排除技巧是卸载您拥有的所有 Visual Studio 内容,然后重新启动。 Then install the latest version of Visual Studio.然后安装最新版本的 Visual Studio。 Then install any other SDKs that you need via the Visual Studio installer wizard.然后通过 Visual Studio 安装程序向导安装你需要的任何其他 SDK。 If you require to have Visual Studio 2015 and 2019 installed, go ahead and install 2015 before you install 2019.如果您需要安装 Visual Studio 2015 和 2019,请继续安装 2015,然后再安装 2019。

If you want to change the project templates, you can in fact do that.如果您想更改项目模板,您实际上可以这样做。 The folder for the default templates is here:默认模板的文件夹在这里:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\ProjectTemplates\VC\WindowsDesktop\

Let's say you want to modify the ConsoleApplication template.假设您要修改 ConsoleApplication 模板。 Create a new project, in this new project edit the Project Property "SDK version" to be 8.1.创建一个新项目,在这个新项目中编辑项目属性“SDK版本”为8.1。 Save the project and then go to Project->Export Template.保存项目,然后转到项目->导出模板。 Export the template.导出模板。 It will be a zip file in Documents\Visual Studio 2019\My Exported Templates.它将是 Documents\Visual Studio 2019\My Exported Templates 中的一个 zip 文件。 Unzip it.解压它。

You will notice the.vstemplate file in this folder which is similar to the found in the directory above.您会注意到此文件夹中的 .vstemplate 文件与上述目录中的文件类似。 You will see in the node it will reference a.vcxproj file.您将在节点中看到它将引用 a.vcxproj 文件。 In this.vcxproj file you will find the property:在 this.vcxproj 文件中,您将找到以下属性:

<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>

Therefore in order to modify the default templates to match the template you just exported you will need to add the necessary files and lines XML from the My Exported Templates files and overwrite the defaults in Program Files.因此,为了修改默认模板以匹配您刚刚导出的模板,您需要从我的导出模板文件中添加必要的文件和行 XML,并覆盖程序文件中的默认值。 These will probably be overwritten each time you update VS tho.每次更新 VS 时,这些可能都会被覆盖。

Alternatively just extract the zip file of template you exported to the folder:或者,只需将导出的模板的 zip 文件解压缩到文件夹中:

ProjectTemplates\VC\WindowsDesktop\ ProjectTemplates\VC\WindowsDesktop\

And you will find it in your templates after you reload visual studio重新加载 visual studio 后,您会在模板中找到它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM