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

问题描述

当我使用C#项目时,我已经制作了一个NuGet软件包。它在 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 ++,我如何实际修改我的包,以便可以在C ++ / CLI项目中将DLL添加为托管引用?我找不到任何解释这个的教程。如果我尝试仅仅添加包,我会收到以下错误:

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 在build / native下的文件,但是我需要把这些文件放在哪里才能使其工作?

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

有一个完整的例子,这里是添加 1.0.0在Visual Studio 2013 C ++ / CLI项目中:

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. 如果尚未打开,请打开软件包管理器控制台(工具> NuGet软件包管理器>软件包管理器控制台

  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.0encoding =utf-8?>
< packages>
< package id =NuGet.CommandLineversion =2.8.6/>
< / packages>


  • 在文本编辑器(如记事本++)中,添加一个 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" />
    

    ..在< packages> 元素。

    打开命令提示符(我打开了一个VS2013 Developer命令提示符,但常规命令提示符应该可以正常使用。)

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

    运行以下命令,更改版本号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项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    09-03 07:26