I have made a NuGet package that works well when I use it from a C# project. It contains a DLL in the lib/net40
directory, and the DLL gets added as a reference.
Now that NuGet supports C++, how do I actually modify my package so that the DLL can be added as a managed reference in a C++/CLI project? I can't find any tutorials explaining this. If I try to just add the package as is, I get the following error:
You are trying to install this package into a project that targets 'Native,Version=v0.0', but the package does not contain any assembly references or content files that are compatible with that framework.
One would think that the solution is to put the files under lib/native, but according to http://docs.nuget.org/docs/reference/support-for-native-projects, that is not supported. Also, simply putting the DLL directly under lib doesn't seem to do anything.
Apparently, I am supposed to do this with a .props
or .targets
file under build/native, but what do I need to put into those files to make this work ?
As Patrick O'Hara wrote, NuGet will not make changes to a C++/CLI project for you. See GitHub Issue NuGet/Home#1121 - Cannot install managed packages into a CLI project. However, using the NuGet command line utility,
NuGet.exe
, you can have NuGet download and unpack the desired package(s).For a complete example, here were steps that I took to add a reference to OptimizedPriorityQueue 1.0.0 in a Visual Studio 2013 C++/CLI project:
In the Package Manager Console, install the NuGet.CommandLine package:
(Note: As of this writing, the latest version of NuGet.CommandLine is 2.8.6. It may be different for you.)
Within your project folder, there should now be a
.nuget\packages.config
XML file with the following contents:In a text editor such as Notepad++, add a
<package>
element for the desired package. In this case, I added:.. within the
<packages>
element.Open a command prompt (I opened a VS2013 Developer Command Prompt, but a regular command prompt should work.)
cd
into the project folder.Run the following command, changing the version number of NuGet.CommandLine if different:
For me, the output was:
packages
subdirectory of your project folder and click the Add button. Click OK to close the Add Reference dialog.You should now be able to use the assembly in your C++/CLI project:
There seem to be actually a possibility to enable "regular" NuGet packages to be installed and automatically referenced from C++/CLI projects using following steps (at least with
NuGet >= 2.5
):Add (or modify) a
build\<ProjectName>.targets
file to your project to be packaged and put following content into it (make sure to replace<AssemblyName>
with an actual value):In the
.nuspec
of the packaged project add one or morefile
entries to also place the assembly inlib\native\
directory at the target machine:Even if NuGet does not add assembly references to C++/CLI projects, it still inserts any
.props
and.targets
files provided by a package. And the custom target from step 1 will add a reference to our packaged assembly.One drawback of this solution, as far as I could see it, is that the reference added in such a way is not displayed in the
Commpon Properties/Framework and References
section of the C++/CLI project. There may also be others, so use it at your own risk...Credentials are actually encrypted with the machinekey where the package source was added. Unless using the plaintext variant, the setApiKey command should probably be run as part of the build.
As mentioned in the answer to this port (Nuget won't install Entity Framework into C++/CLI project), NuGet will not make the changes to a C++/CLI project for you. It will however download and unpackage the dependency for you. We use it from the command line as part of our make dependencies. The command line will look something like this:
Note that the command line areguments are separated one to a line to make reading easier. Also we decided to check NuGet into our source control un the .NuGet folder. The goal was to make it easier to setup a build machine for our various environments (not all of which use Visual Studio). Once you have run this command for the first time, you must manually add the dependencies to your C++/CLI project.
Hope that helps.
The installer tries to add a reference to itself in the C# startup project. Make a C# project the startup project in the solution before install. Create a dummy C# project if you do not have one