【问题标题】:Consuming nuget package made with .nuspec and .targets (C++)使用 .nuspec 和 .targets (C++) 制作的 nuget 包
【发布时间】:2021-05-11 21:53:16
【问题描述】:

努力将已编译的 c++ dll(x86 和 x64)打包以便 C# 库可以使用它。

管理使用 nuspec 文件打包和推送 dll,但是当使用 VS2019 包管理器时,它成功安装了包,但是没有出现引用。 (任何 CPU)

.nuspec

<?xml version="1.0"?>
<package >
    <metadata>
        <id>component1</id>
        <version>1.0.0</version>
        <description>mycomponent</description>
        <authors>Me</authors>
    </metadata> 
    <files>
        <file src="32\component1.dll"   target="build\x86" />
        <file src="64\component1.dll"   target="build\x64" />
        <file src="component1.targets"   target="lib\net40" />
    </files>
</package>

由于消费项目的目标是 .NET 4.0,我创建了一个指向同一框架的 component1.targets 文件

.目标

<ItemGroup Condition=" '$(Platform)' == 'x64' ">
    <Reference Include="component1">
              <HintPath>"$(MSBuildThisFileDirectory)..\..\build\x64\component1.dll"</HintPath>
    </Reference>
</ItemGroup>

<ItemGroup Condition=" '$(Platform)' == 'x86' OR '$(Platform)' == 'AnyCPU' OR '$(Platform)' == 'Any CPU' ">
    <Reference Include="component1">
              <HintPath>$(MSBuildThisFileDirectory)..\..\build\x32\component1.dll</HintPath>
    </Reference>
</ItemGroup>

【问题讨论】:

  • $(Platform)' == 'AnyCPU' 声明了两次,你在包文件中有target="build\x86",但在ItemGroup 中有\build\x32
  • 您可能还需要在您的 csproj 文件中添加一个 itemgroup 来将本机 dll 复制到输出目录。
  • 你好 gumby,如果我的回答能帮助你解决问题,请不要忘记accept it
  • 嗨 gumby,有关于这个问题的最新消息吗?

标签: c# c++ visual-studio nuget nuspec


【解决方案1】:

你的脚步一团糟。

您应该注意,目标文件应命名为 .targets`,与 nuget 包 id 同名,否则将无法工作。见this link

另外,目标文件应该放在nupkgbuild文件夹中。

这是两个重要提示。

1) 请将您的 nuspec 文件更改为:

<?xml version="1.0"?>
<package >
    <metadata>
        <id>component1</id>
        <version>1.0.0</version>
        <description>mycomponent</description>
        <authors>Me</authors>
    </metadata> 
    <files>
        <file src="32\component1.dll"   target="build\x86" />
        <file src="64\component1.dll"   target="build\x64" />
        <file src="component1.targets"   target="build" />
    </files>
</package>

2) 然后,将您的component1.targets 更改为:

您应该删除"$(MSBuildThisFileDirectory)..\..\build\x64\component1.dll" 下的""

<Project>

<ItemGroup Condition=" '$(Platform)' == 'x64' ">
    <Reference Include="component1">
              <HintPath>$(MSBuildThisFileDirectory)..\build\x64\component1.dll</HintPath>
    </Reference>
</ItemGroup>

<ItemGroup Condition=" '$(Platform)' == 'x86' OR '$(Platform)' == 'AnyCPU' OR '$(Platform)' == 'Any CPU' ">
    <Reference Include="component1">
              <HintPath>$(MSBuildThisFileDirectory)..\build\x32\component1.dll</HintPath>
    </Reference>
</ItemGroup>

</Project>

3) 使用nuget pack 重新打包nuget 包。在安装这个新版本的 nuget 包之前,请clean nuget caches first 或删除C:\Users\xxx(current user name)\.nuget\packages 下的所有文件。

它在我这边效果很好。

我的 nuget 包名为 testt,我在 x64 下引用了 ClassLibrary21.dll

【讨论】:

  • 这行得通,但我认为我必须针对框架版本。我猜测通过将 .targets 文件放在构建文件夹中,无论使用框架如何,它都会将其拾取。非常感谢您的详细回答
猜你喜欢
  • 2015-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-17
  • 2017-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多