如何在 .NET Core 项目中添加 C++ 库 [英] How to add C++ library in a .NET Core project

查看:52
本文介绍了如何在 .NET Core 项目中添加 C++ 库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在.NET Core 项目(类库)中添加C++ 库.我尝试创建一个 nuget 包但不起作用.我收到此错误:

How is the way to add a C++ Library in a .NET Core project (Class Library). I tried creating a nuget package but doesn't work. I got this error:

NameOfDll.dll"中发生类型为System.DllNotFoundException"的未处理异常

当我添加 nuget 包时,project.json 添加以下引用:

When I add the nuget package the project.json add the following reference:

  "dependencies": {
    "Core": "1.0.0-*",
    "NETStandard.Library": "1.6.0",
    "NameOfDll.dll": "1.0.0"
  },

推荐答案

dependencies 属性在 project.json 中指定包依赖,因为这个 NuGet 处理 NameOfDll.dll 作为包 ID,但不是作为 dll 名称.

dependencies attribute in project.json specifies package dependencies, because of this NuGet handles NameOfDll.dll as a package ID, but not as a dll name.

要将本机 C++ dll 添加到 .xproj 库的 NuGet 包中,您应该执行以下步骤:

To add native C++ dlls into you NuGet package for .xproj library you should do the following steps:

  1. 将您的NameOfDll.dll 放在lib 目录中,靠近MyClassLibrary.xproj
  2. 打开 project.json 文件并在那里添加:

  1. Put your NameOfDll.dll in lib directory near MyClassLibrary.xproj
  2. Open project.json file and add there:

"packOptions": {
  "files" : {
    "includeFiles" : "lib/NameOfDll.dll"
  }
}

  • 执行dotnet pack

    NameOfDll.dll 将包含在路径 libNameOfDll.dll 下的 NuGet 包中.

    NameOfDll.dll will be included into NuGet package under the path libNameOfDll.dll.

    要将本机 C++ dll 添加到 .csproj 库的 NuGet 包中,您应该执行以下步骤:

    To add native C++ dlls into your NuGet package for .csproj library you should do the following steps:

    1. 我假设您已经管理了名为 MyClassLibrary.csproj 的项目.
    2. 使用 nuget spec 命令为您的类库创建新的 NuGet 包.
    3. MyClassLibrary 附近创建 libuildcontent ools 目录.nuspec.
    4. 将所有本机 dll 复制到 uild 文件夹中.
    5. 将复制的本机 dll 的扩展名更改为 .native.
    6. uild 文件夹中使用以下内容创建 MyClassLibrary.targets:

    1. I assume you have managed project with name MyClassLibrary.csproj.
    2. Create new NuGet package for you class library with nuget spec command.
    3. Create lib, uild, content and ools directories near MyClassLibrary.nuspec.
    4. Copy all your native dlls into the uild folder.
    5. Change extension of copied native dlls to .native.
    6. Create MyClassLibrary.targets with the following content inside uild folder:

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <ItemGroup>
        <AvailableItemName Include="NativeBinary" />
      </ItemGroup>
      <ItemGroup>
        <NativeBinary Include="$(MSBuildThisFileDirectory)*">
          <TargetPath></TargetPath>
        </NativeBinary>
      </ItemGroup>
      <PropertyGroup>
        <PrepareForRunDependsOn>
          $(PrepareForRunDependsOn);
          CopyNativeBinaries
        </PrepareForRunDependsOn>
      </PropertyGroup>
      <Target Name="CopyNativeBinaries" DependsOnTargets="CopyFilesToOutputDirectory">
        <Copy SourceFiles="@(NativeBinary)"
              DestinationFiles="@(NativeBinary->'$(OutDir)\%(TargetPath)\%(Filename).dll')"
              Condition="'%(Extension)'=='.native'">
          <Output TaskParameter="DestinationFiles" ItemName="FileWrites" />
        </Copy>
        <Copy SourceFiles="@(NativeBinary)"
              DestinationFiles="@(NativeBinary->'$(OutDir)\%(TargetPath)\%(Filename).%(Extension)')"
              Condition="'%(Extension)'!='.native'">
          <Output TaskParameter="DestinationFiles" ItemName="FileWrites" />
        </Copy>
      </Target>
    </Project>
    

    提示:.targets 内容取自 这个 问题.上述 .targets 文件将在安装 NuGet 包时注入到目标项目文件中.

    Hint: The .targets content is taken from this question. The above .targets file will be injected on an installation of the NuGet package into the target project file.

    将以下行添加到您的 MyClassLibrary.nuspec 中:

    Add the following lines into your MyClassLibrary.nuspec:

    <files>
      <file src="lib" target="lib" />
      <file src="tools" target="tools" />
      <file src="content" target="content" />
      <file src="build" target="build" />
    </files>
    

  • 执行 nuget pack MyClassLibrary.csproj 以构建您的 NuGet 包.

  • Execute nuget pack MyClassLibrary.csproj to build your NuGet package.

    这篇关于如何在 .NET Core 项目中添加 C++ 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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