首页 > 解决方案 > 使用 .nuspec 和 .targets (C++) 制作的 nuget 包

问题描述

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

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

.nu​​spec

<?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 文件

.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>

标签: c#c++visual-studionugetnuspec

解决方案


你的脚步一团糟。

您应该注意,目标文件应命名为 <package_id>.targets`,与 nuget 包 ID 同名,否则将无法工作。请参阅此链接

此外,目标文件应放入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 包之前,请先清理 nuget 缓存或删除C:\Users\xxx(current user name)\.nuget\packages.

它在我身边运作良好。

我的 nuget 包被称为testt,我ClassLibrary21.dllx64.

在此处输入图像描述


推荐阅读