将NuGet包中的本机文件添加到项目输出目录 [英] Add native files from NuGet package to project output directory

查看:586
本文介绍了将NuGet包中的本机文件添加到项目输出目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为一个.net程序集创建NuGet程序包,该程序集不会拼写到本机的win32 dll. 我需要将程序集和本机dll都打包,并将程序集添加到项目引用中(此部分没有问题),并且本机dll应复制到项目输出目录或其他相对目录中.

I'm trying to create NuGet package for a .Net assembly which does pinvoke to a native win32 dll. I need to pack both the assembly and the native dll with the assembly added to the project references (no problem at this part) and the native dll should be copied into the project output directory or some other relative directory.

我的问题是:

  1. 如何在不使用Visual Studio尝试将其添加到引用列表的情况下打包本机dll?
  2. 我是否需要编写install.ps1来复制本机dll?如果可以,我如何访问包装内容以进行复制?

推荐答案

在目标文件中使用Copy目标复制所需的库不会将这些文件复制到引用该项目的其他项目,从而导致.不过,这可以通过使用None元素,使用更简单的目标文件来完成,因为MSBuild会将所有None文件复制到引用项目中.

Using the Copy target in the targets file to copy required libraries won't copy those files to other projects which reference the project, resulting in a DllNotFoundException. This can be done with a much simpler targets file though, using a None element, as MSBuild will copy all None files to referencing projects.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <NativeLibs Include="$(MSBuildThisFileDirectory)**\*.dll" />
    <None Include="@(NativeLibs)">
      <Link>%(RecursiveDir)%(FileName)%(Extension)</Link>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>
</Project>

将目标文件以及所需的本机库添加到nuget软件包的build目录中.目标文件将包括build目录的所有子目录中的所有dll文件.因此,要添加由Any CPU托管程序集使用的本机库的x86x64版本,您将得到一个类似于以下内容的目录结构:

Add the targets file to the build directory of the nuget package along with the required native libraries. The targets file will include all dll files in all child directories of the build directory. So to add an x86 and x64 version of a native library used by an Any CPU managed assembly you would end up with a directory structure similar to the following:

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