如何让我的托管 NuGet 包支持 C++/CLI 项目? [英] How can I make my managed NuGet package support C++/CLI projects?

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

问题描述

我制作了一个 NuGet 包,当我在 C# 项目中使用它时效果很好.它在 lib/net40 目录中包含一个 DLL,该 DLL 被添加为引用.

既然 NuGet 支持 C++,我该如何实际修改我的包,以便可以将 DLL 作为托管引用添加到 C++/CLI 项目中?我找不到任何解释这个的教程.如果我尝试按原样添加包,则会出现以下错误:

<块引用>

您正在尝试将此包安装到面向Native,Version=v0.0"的项目中,但该包不包含任何与该框架兼容的程序集引用或内容文件.

有人会认为解决方案是将文件放在 lib/native 下,但根据 http://docs.nuget.org/docs/reference/support-for-native-projects,不支持.此外,简单地将 DLL 直接放在 lib 下似乎没有任何作用.

显然,我应该使用 build/native 下的 .props.targets 文件来执行此操作,但是我需要将什么放入这些文件中才能制作这项工作?

解决方案

正如 Patrick O'Hara 所写,NuGet 将不要为您更改 C++/CLI 项目.请参阅GitHub 问题 NuGet/Home#1121 - 无法将托管包安装到 CLI 项目中.但是,使用 NuGet 命令行实用程序 NuGet.exe,您可以让 NuGet 下载并解压所需的包.

作为一个完整的例子,这里是我添加对 OptimizedPriorityQueue 的引用所采取的步骤Visual Studio 2013 C++/CLI 项目中的 1.0.0:

  1. 如果尚未打开包管理器控制台,请打开(工具 > NuGet 包管理器 > 包管理器控制台).
  2. 在包管理器控制台中,安装 NuGet.CommandLine 包:

    <前>安装包 NuGet.CommandLine

    (注意:在撰写本文时,NuGet.CommandLine 的最新版本是 2.8.6.它可能因您而异.)

  3. 在您的项目文件夹中,现在应该有一个 .nugetpackages.config XML 文件,其中包含以下内容:

    <包><package id="NuGet.CommandLine" version="2.8.6"/></包>

  4. 在 Notepad++ 等文本编辑器中,为所需的包添加 元素.在这种情况下,我补充说:

    .. 在 元素中.

  5. 打开命令提示符(我打开了 VS2013 开发人员命令提示符,但常规命令提示符应该可以工作.)

  6. cd 进入项目文件夹.
  7. 运行以下命令,如果不同,请更改 NuGet.CommandLine 的版本号:

    <前>.packagesNuGet.CommandLine.2.8.6 oolsNuGet.exe 安装 -NonInteractive -OutputDirectory 包 .nugetpackages.config

    对我来说,输出是:

    <前>安装OptimizedPriorityQueue 1.0.0.0".已成功安装OptimizedPriorityQueue 1.0.0.0".package.config 中列出的所有软件包都已安装.

  8. 右键单击 Visual Studio 中的项目并选择属性.在通用属性 > 引用下,点击添加新引用…按钮.
  9. 选择左侧的浏览.在添加引用"对话框的确定"和取消"按钮旁边,有一个浏览…按钮.单击该按钮可打开文件选择对话框.
  10. 导航到 NuGet 解压到项目文件夹的 packages 子目录中的 DLL,然后单击添加按钮.单击确定关闭添加引用"对话框.
  11. 您现在应该可以在您的 C++/CLI 项目中使用该程序集了:

    使用命名空间Priority_Queue;//...

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:

  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.6	oolsNuGet.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;
    
    //...
    

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆