Skip to content

Instantly share code, notes, and snippets.

@rfuzzo
Last active July 18, 2021 07:13
Show Gist options
  • Save rfuzzo/67ff76f0e4bff4fe45ce0b1005328b84 to your computer and use it in GitHub Desktop.
Save rfuzzo/67ff76f0e4bff4fe45ce0b1005328b84 to your computer and use it in GitHub Desktop.
Native c++/cli nugets

Native c++/cli nugets

You need:

  • nuget.exe
  • VS developer command prompt

Nuget Creation:

  • Create a ./build folder
  • Create MYCLRPROJECT.props inside the build folder (file below)
  • Create MYCLRPROJECT.targets inside the build folder (file below)
  • Create a MYCLRPROJECT.nuspec file, where you explicitly include at least: MYCLRPROJECT.dll, Ijwhost.dll from the build output folder and at least MYCLRPROJECT.props and MYCLRPROJECT.targets from the ./build folder (file below)
  • Build the MYCLRPROJECT.vcxproj
  • nuget pack the nuspec file

This example builds to $(ProjectDir)bin\$(Platform)\$(Configuration)\

@echo off
set vs_com19="C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\vsdevcmd.bat"
@RD /S /Q .\bin\
call %vs_com19%
call msbuild -t:restore /t:Build /p:Configuration=Debug /p:Platform=x64 /p:PublishProfile=x64
call msbuild -t:restore /t:Build /p:Configuration=Debug /p:Platform=x86 /p:PublishProfile=x86
nuget pack .\MYCLRPROJECT.nuspec -Build -Symbols -NoPackageAnalysis -Properties 'Configuration=Debug;CConfiguration=Debug;' -OutputDirectory nuget -Version 0.1
echo "finished"
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata minClientVersion="2.5">
<tags>Native, native</tags>
</metadata>
<files>
<!-- Include everything in \build -->
<file src="build\**" target="build" />
<file src="bin\x64\$CConfiguration$\*.dll" target="runtimes\win-x64\native" />
<file src="bin\win32\$CConfiguration$\*.dll" target="runtimes\win-x86\native" />
</files>
</package>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Reference Include="MYCLRDLL" Condition="'$(Platform)' == 'x64' or '$(Platform)' == 'AnyCPU'">
<HintPath>$(MSBuildThisFileDirectory)\..\runtimes\win-x64\native\MYCLRPROJECT.dll</HintPath>
</Reference>
<Reference Include="MYCLRDLL" Condition="'$(Platform)' == 'x86'">
<HintPath>$(MSBuildThisFileDirectory)\..\runtimes\win-x86\native\MYCLRPROJECT.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<NativeLibs Include="$(MSBuildThisFileDirectory)\..\runtimes\win-x86\native\*.dll" Condition="'$(Platform)' == 'x86'" />
<NativeLibs Include="$(MSBuildThisFileDirectory)\..\runtimes\win-x64\native\*.dll" Condition="'$(Platform)' == 'x64' or '$(Platform)' == 'AnyCPU'" />
<ContentWithTargetPath Include="@(NativeLibs)">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<TargetPath>%(Filename)%(Extension)</TargetPath>
</ContentWithTargetPath>
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment