如何修复缺少 NuGet 包的错误?

Kul*_*Gin 3 git visual-studio nuget nuget-package nuget-package-restore

我在 Git 下的 Visual Studio 中有一个 C++ 控制台应用程序,其中有一个名为LibrarySubmoduleSolution. 这LibrarySubmoduleSolution使用 Windows 实现库 NuGet 包。

在我的主解决方案中下载 Git 子模块VCPPConsoleApplicationSolution并在解决方案中恢复 NuGet 包后,我收到以下错误:

Severity Code Description Project File Line
Suppression State Error This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is ..\packages\Microsoft.Windows.ImplementationLibrary.1.0.200519.2\build\native\Microsoft.Windows.ImplementationLibrary.targets. LibrarySubmoduleSolution
D:\Projects\Tests\VCPPConsoleApplicationSolution\LibrarySubmoduleSolution\LibrarySubmoduleSolution\LibrarySubmoduleSolution.vcxproj 166 Severity Code Description Project File Line
Suppression State Error C1083 Cannot open include file: 'wil/resource.h': No such file or directory
VCPPConsoleApplicationSolution
D:\Projects\Tests\VCPPConsoleApplicationSolution\LibrarySubmoduleSolution\LibrarySubmoduleSolution\SomeClass.h 3 在此输入图像描述

I searched for answers on Google and here on Stackoverflow and even after I edit my LibrarySubmoduleSolution.vcxproj file and change packages folder from ..\packages to $(SolutionDir)\packages:

  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
  <ImportGroup Label="ExtensionTargets">
    <Import Project="$(SolutionDir)\packages\Microsoft.Windows.ImplementationLibrary.1.0.200519.2\build\native\Microsoft.Windows.ImplementationLibrary.targets" Condition="Exists('$(SolutionDir)\packages\Microsoft.Windows.ImplementationLibrary.1.0.200519.2\build\native\Microsoft.Windows.ImplementationLibrary.targets')" />
  </ImportGroup>
  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
    <PropertyGroup>
      <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
    </PropertyGroup>
    <Error Condition="!Exists('$(SolutionDir)\packages\Microsoft.Windows.ImplementationLibrary.1.0.200519.2\build\native\Microsoft.Windows.ImplementationLibrary.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\packages\Microsoft.Windows.ImplementationLibrary.1.0.200519.2\build\native\Microsoft.Windows.ImplementationLibrary.targets'))" />
  </Target>
Run Code Online (Sandbox Code Playgroud)

I still can't build with the error:

Severity Code Description Project File Line Suppression State Error C1083 Cannot open include file: 'wil/resource.h': No such file or directory VCPPConsoleApplicationSolution D:\Projects\Tests\VCPPConsoleApplicationSolution\LibrarySubmoduleSolution\LibrarySubmoduleSolution\SomeClass.h 3 在此输入图像描述

Only after I install Windows Implementation Library NuGet package in my main console application project, it starts to build without errors.

以下是快速重现错误的主模块和子模块解决方案: https://github.com/KulaGGin/VCPPConsoleApplicationSolution https://github.com/KulaGGin/LibrarySubmoduleSolution

Mr *_*ian 5

如何修复缺少 NuGet 包的错误?

我从github下载了你的项目并遇到了与你描述的相同的问题。

主要问题是您的 nuget 包已损坏,或者引用 Nuget 包的项目存在问题。

请按照以下步骤操作,我通过它们解决了问题。

建议

1) 清理所有nuget缓存或只删除下面的所有文件C:\Users\xxx(current user name)\.nuget\packages

2)关闭VS实例,删除.vs解决方案文件夹下的隐藏文件夹和任何输出文件夹(如x64文件夹)

3)在Tools --> Nuget Package Manager --> Package Manager Consoleupdate-package -reinstall下运行命令以在项目中重新安装该包。

然后,错误就会消失。我遵循这些并最终得到它。

更新1

===========================

很抱歉没有详细观察您的问题。问题出在xxx.vcxproj文件中。

问题出在下面:

<Error Condition="!Exists('$(SolutionDir)\packages\Microsoft.Windows.ImplementationLibrary.1.0.200519.2\build\native\Microsoft.Windows.ImplementationLibrary.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\packages\Microsoft.Windows.ImplementationLibrary.1.0.200519.2\build\native\Microsoft.Windows.ImplementationLibrary.targets'))" />

 <Import Project="$(SolutionDir)\packages\Microsoft.Windows.ImplementationLibrary.1.0.200519.2\build\native\Microsoft.Windows.ImplementationLibrary.targets" Condition="Exists('$(SolutionDir)\packages\Microsoft.Windows.ImplementationLibrary.1.0.200519.2\build\native\Microsoft.Windows.ImplementationLibrary.targets')" />
Run Code Online (Sandbox Code Playgroud)

我想知道你为什么要使用$(SolutionDir)而不是...

在你的问题中$(SolutionDir)\packages意思是xxx(SolutionName)\\packages

$(SolutonDir)意味着xxxx\LibrarySubmoduleSolution\并且它已经包含\.

解决方案

请删除和\之间的内容并更改为:$(SolutionDir)packages

$(SolutionDir)packages\Microsoft.Windows.ImplementationLibrary.1.0.200519.2\build\native\Microsoft.Windows.ImplementationLibrary.targets在两个 xml 节点中。

================================

更新2

另外,如果你的主工程VCPPConsoleApplicationSolution引用并使用了它的文件,那么你还应该在主工程中包含这个库路径,否则在主工程中LibrarySubmoduleSolution是找不到的。resource.hVCPPConsoleApplicationSolution

实际上,当你在主项目中引用someclass.hfrom时,该文件会被复制到,在这样的环境中,当你安装它时,并没有这个库。在主项目中,它没有安装那个nuget包(自动包含包含路径),所以会发生错误。LibrarySubmoduleSolutionVCPPConsoleApplicationSolutionsomeclass.hVCPPConsoleApplicationSolutionwil/resource.hLibrarySubmoduleSolutionresource.h

作为建议

您可以右键单击VCPPConsoleApplicationSolution属性--> VC++ Dirctories--> 添加xxxx\VCPPConsoleApplicationSolution\packages\Microsoft.Windows.ImplementationLibrary.1.0.200519.2\includeInclude Directories.

或者在主项目中安装该 nuget 包以自动包含库路径。