本文介绍了如何使我的受管NuGet包支持C ++ / CLI项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了一个NuGet包,当我从一个C#项目使用它工作得很好。它在 lib / net40 目录中包含一个DLL,并将该DLL添加为引用。

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.

NuGet支持C ++,我如何实际修改我的包,使得DLL可以作为C ++ / CLI项目中的托管引用添加?我找不到任何教程解释这个。如果我试图只是添加包作为,我得到以下错误:

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:

有人会认为解决方案是将文件放在lib / native下,但根据,不支持。此外,简单地将DLL直接放在lib下似乎没有做任何事情。

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.

显然,我应该用一个 .props .targets 文件,但是我需要将这些文件放入哪些文件才能使此工作?

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 ?

推荐答案

由于, NuGet不会为您更改C ++ / CLI项目。请参见。但是,使用NuGet命令行实用程序 NuGet.exe ,您可以让NuGet下载并解压所需的软件包。

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).

对于一个完整的示例,这里是我添加一个参考到 OptimizedPriorityQueue 1.0.0:

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. 打开软件包管理器控制台

  2. 在软件包管理器控制台中,安装NuGet.CommandLine软件包:

  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

(注意:最新版本的NuGet.CommandLine是2.8.6,它可能会有所不同。)

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

在您的项目文件夹中,现在应该有一个 .nuget\packages.config 具有以下内容的XML文件:

Within your project folder, there should now be a .nuget\packages.config XML file with the following contents:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="NuGet.CommandLine" version="2.8.6" />
</packages>


  • 在Notepad ++等文本编辑器中,添加 package> 元素。在这种情况下,我添加了:

  • 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.

    打开命令提示符(我打开了一个VS2013开发人员命令提示符,但正常的命令提示符应该可以正常工作。)

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

    执行以下命令,将版本号更改为

  • cd NuGet.CommandLine如果不同:

    Run the following command, changing the version number of NuGet.CommandLine if different:

    
    .\packages\NuGet.CommandLine.2.8.6\tools\NuGet.exe Install -NonInteractive -OutputDirectory packages .nuget\packages.config
    

    对我来说,输出是:

    
    Installing 'OptimizedPriorityQueue 1.0.0.0'.
    Successfully installed 'OptimizedPriorityQueue 1.0.0.0'.
    All packages listed in packages.config are already installed.
    


  • 在Visual Studio中右键单击项目,然后选择属性。在常用属性>参考下,点击添加新参考和翻译按钮。

  • 左手边。在添加参考对话框的确定​​和取消按钮旁边,有一个浏览… 按钮。

  • 导航到NuGet解压到项目文件夹的子目录的DLL,然后单击添加按钮。点击确定关闭添加引用对话框。

  • 您现在应该可以在C ++ / CLI项目中使用程序集:

  • Right click on the project in Visual Studio and select Properties. Under Common Properties > References, click the Add New Reference… button.
  • 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.
  • 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.
  • You should now be able to use the assembly in your C++/CLI project:

    using namespace Priority_Queue;
    
    //...
    


  • 这篇关于如何使我的受管NuGet包支持C ++ / CLI项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!