Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

.net - How can I make my managed NuGet package support C++/CLI projects?

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

  1. Open the Package Manager Console if not already open (TOOLS > NuGet Package Manager > Package Manager Console).
  2. In the Package Manager Console, install the NuGet.CommandLine package:

    Install-Package NuGet.CommandLine
    

    (Note: As of this writing, the latest version of NuGet.CommandLine is 2.8.6. It may be different for you.)

  3. Within your project folder, there should now be a .nugetpackages.config XML file with the following contents:

    <?xml version="1.0" encoding="utf-8"?>
    <packages>
      <package id="NuGet.CommandLine" version="2.8.6" />
    </packages>
    
  4. In a text editor such as Notepad++, add a <package> element for the desired package. In this case, I added:

    <package id="OptimizedPriorityQueue" version="1.0.0" />
    

    .. within the <packages> element.

  5. Open a command prompt (I opened a VS2013 Developer Command Prompt, but a regular command prompt should work.)

  6. cd into the project folder.
  7. Run the following command, changing the version number of NuGet.CommandLine if different:

    .packagesNuGet.CommandLine.2.8.6oolsNuGet.exe Install -NonInteractive -OutputDirectory packages .nugetpackages.config
    

    For me, the output was:

    Installing 'OptimizedPriorityQueue 1.0.0.0'.
    Successfully installed 'OptimizedPriorityQueue 1.0.0.0'.
    All packages listed in packages.config are already installed.
    
  8. Right click on the project in Visual Studio and select Properties. Under Common Properties > References, click the Add New Reference… button.
  9. Select Browse on the left hand side. Next to the Add Reference dialog's OK and Cancel buttons, there is a Browse… button. Click that to open a file selection dialog.
  10. Navigate to the DLLs that NuGet unpacked to the packages subdirectory of your project folder and click the Add button. Click OK to close the Add Reference dialog.
  11. You should now be able to use the assembly in your C++/CLI project:

    using namespace Priority_Queue;
    
    //...
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...