-1

I've read these instructions for creating NuGet packages for C++/CLI .vcxproj projects using a .nuspec file. However, I'd like to use MSBuild to create my package instead of using a .nuspec file. I've seen these instructions for packaging .NET Framework projects using MSBuild, but I haven't been able to find anything for doing the same with .vcxproj files.

watkipet
  • 959
  • 12
  • 23

1 Answers1

-1

MSBuild can pack a Visual Studio project into a NuGet package, but only if that project uses PackageReference instead of packages.config.

As of this writing, the Visual Studio UI doesn't allow you to choose PackageReference instead of packages.config as the NuGet package configuration mechanic for VCXPROJ files. However, you can manually edit the .vcxproj file and convert it to PackageReference format.

Edit .vcxproj

By default, .vcxproj files are not SDK-style projects (I don't know if they can even be converted to SDK-style projects). To build NuGet packages from non-SDK-style projects, you'll need to enable managed package reference support and add reference NuGet.Build.Tasks.Pack.

Enable Managed Package Reference Support

Add the following to your .vcxproj (inside the <PropertyGroup Label="Globals"> element):

<EnableManagedPackageReferenceSupport>true</EnableManagedPackageReferenceSupport>

Reference NuGet.Build.Tasks.Pack

Add the following to your .vcxproj (inside the Project element):

  <ItemGroup>
    <PackageReference Include="NuGet.Build.Tasks.Pack">
      <Version>5.11.0</Version>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>

Further Configuration

Add any other packages your project depends on. Notice the <PrivateAssets>all</PrivateAssets> element. You need NuGet.Build.Tasks.Pack to pack your project, but you don't want it as a dependency when others try and install your NuGet package. Adjust the version for NuGet.Build.Tasks.Pack as appropriate.

If your project references static libraries, you don't want those included as NuGet package dependencies. Use the PrivateAssets element to exclude them. I.e:

  <ItemGroup>
    <ProjectReference Include="..\vendor\static-lib-build\static-lib\static-lib.vcxproj">
      <Project>{8aa99891-3ff7-3fa7-b1b5-131664298a26}</Project>
      <PrivateAssets>all</PrivateAssets>
    </ProjectReference>
  </ItemGroup>

Also add the following inside the Project element"

  <PropertyGroup>
    <PackageId>Your.Package.Id</PackageId>
    <Authors>Your Name</Authors>
    <BuildOutputTargetFolder>lib</BuildOutputTargetFolder>
    <PackageDescription>A description of this package.</PackageDescription>
    <TargetFramework>net461</TargetFramework>
  </PropertyGroup>

What's in the TargetFramework element should correspond to what's in the TargetFrameworkVersion element elsewere in your project.

Note that I haven't covered building a cross-platform NuGet package. By its very nature, C++/CLI projects are targeted to a specific processor / architecture. You can include native libraries for multiple architectures in the runtime directory of your NuGet package, but I don't cover that here.

Restore, Build, and Pack Using MSBuild

This line will restore your NuGet packages, build the project, and then pack it:

"c:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSBuild.exe" path\to\the_project.vcxproj /p:Platform="Any CPU" /p:ProductVersion=1.0.0.69 /t:Restore;Build /t:pack

Change the command line options to suit your needs. You may need to run this command twice if NuGet.Build.Tasks.Pack hasn't already been restored.

watkipet
  • 959
  • 12
  • 23