Nuget - LNK2019 在使用我的一个链接到 nuget 包的项目时

Nuget - LNK2019 when using one of my project that links to a nuget package

好的,我的问题很简单,但我想不通。

在单个解决方案中,我有一个项目(应用程序 exe)引用了另一个项目(lib)。此库引用了一个 nuget 包,例如 CxImage。

.目标

<?xml version="1.0" encoding="utf-8"?>
<Project ToolVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemDefinitionGroup>
    <ClCompile>
      <AdditionalIncludeDirectories>"$(MSBuildThisFileDirectory)include";%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
      <!-- patch -->
      <AdditionalIncludeDirectories>"$(MSBuildThisFileDirectory)include\cximage";%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
    </ClCompile>
  </ItemDefinitionGroup>
  <ItemDefinitionGroup Condition="$(Configuration.ToLower().StartsWith('release')) And '$(Platform.ToLower())'=='x64'">
    <Lib>
      <AdditionalLibraryDirectories>"$(MSBuildThisFileDirectory)lib\x64";%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
    </Lib>
    <Link>
      <AdditionalDependencies>CxImage.lib;jpeg.lib;png.lib;tiff.lib;zlib.lib;%(AdditionalDependencies)</AdditionalDependencies>
      <AdditionalLibraryDirectories>"$(MSBuildThisFileDirectory)lib\x64";%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
    </Link>
  </ItemDefinitionGroup>
  <ItemDefinitionGroup Condition="$(Configuration.ToLower().StartsWith('debug')) And '$(Platform.ToLower())'=='x64'">
    <Lib>
      <AdditionalLibraryDirectories>"$(MSBuildThisFileDirectory)lib\x64";%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
    </Lib>
    <Link>
      <AdditionalDependencies>CxImage_d.lib;jpeg_d.lib;png_d.lib;tiff_d.lib;zlib_d.lib;%(AdditionalDependencies)</AdditionalDependencies>
      <AdditionalLibraryDirectories>"$(MSBuildThisFileDirectory)lib\x64";%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
    </Link>
  </ItemDefinitionGroup>
  <ItemDefinitionGroup Condition="$(Configuration.ToLower().StartsWith('release')) And '$(Platform.ToLower())'=='win32'">
    <Lib>
      <AdditionalLibraryDirectories>"$(MSBuildThisFileDirectory)lib\x86";%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
    </Lib>
    <Link>
      <AdditionalDependencies>CxImage.lib;jpeg.lib;png.lib;tiff.lib;zlib.lib;%(AdditionalDependencies)</AdditionalDependencies>
      <AdditionalLibraryDirectories>"$(MSBuildThisFileDirectory)lib\x86";%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
    </Link>
  </ItemDefinitionGroup>
  <ItemDefinitionGroup Condition="$(Configuration.ToLower().StartsWith('debug')) And '$(Platform.ToLower())'=='win32'">
    <Lib>
      <AdditionalLibraryDirectories>"$(MSBuildThisFileDirectory)lib\x86";%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
    </Lib>
    <Link>
      <AdditionalDependencies>CxImage_d.lib;jpeg_d.lib;png_d.lib;tiff_d.lib;zlib_d.lib;%(AdditionalDependencies)</AdditionalDependencies>
      <AdditionalLibraryDirectories>"$(MSBuildThisFileDirectory)lib\x86";%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
    </Link>
  </ItemDefinitionGroup>
  <ItemGroup />
</Project>

在构建引用此库的其他项目时,会导致在函数

中引用 LNK2019 未解析的外部符号 "public: __thiscall CxImage::CxImage(unsigned int)" (??0CxImage@@QAE@I@Z)

表示找不到cxImage的lib文件。

我找到了解决问题的方法,但我认为这不对。 在其他应用程序中使用的库的属性中,在 Librarian > General > Additionnal Dependencies 中,我添加了 cxImage 库。我虽然 nuget 包背后的第一个想法是避免这种事情所以...

** 与cxImage无关,其他情况均有此问题。例如我刚才提到的这个

有什么想法吗?? ^^

Nuget - LNK2019 when using one of my project that links to a nuget package

您还应该将 nuget 包 CxImage 添加到参考项目中。

项目引用了lib,而lib引用了nuget包CxImage,所以项目没有直接引用nuget包CxImage,Visual Studio/MSBuild 尽量聪明,只将引用 lib 引入项目,而不是将 nuget 包 CxImage 添加到项目。 因为 Visual Studio/MSBuild 不知道项目是否需要间接引用 CxImage。所以 Visual Studio/MSBuild 不会将 nuget 包添加到引用项目中,以避免引用污染项目。您可以参考 similar issue 了解详细信息。

在这种情况下,您将收到错误“找不到 cxImage 的 lib 文件”。要解决此问题,还应将nuget包CxImage添加到引用项目中,以便项目直接引用nuget包CxImage