Struggling to get a compiled c++ dll (both x86 and x64) packaged up so that a C# library can consume it.
Managed to pack and push the dll using nuspec file however when using VS2019 package manager it successfully installs the package however the reference does not appear. (Any 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>
As the consuming project is targeting .NET 4.0 I created a component1.targets file pointing to the same framework
.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>
Your steps are in a mess.
You should note that the targets file should be named as <package_id>.targets`, the same name as the nuget package id or it could not work. See this link.
Also, the targets file should be put into build
folder of nupkg.
That are the two importance tips.
1) Please change your nuspec
file to this:
<?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) Then, change your component1.targets
to these:
You should remove the ""
under "$(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) use nuget pack
to repack the nuget package. And before you install this new version of the nuget package, please clean nuget caches first or just delete all files under C:\Users\xxx(current user name)\.nuget\packages
.
And it works well in my side.
My nuget package is called testt
, and I referenced ClassLibrary21.dll
under x64
.