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

查看:135
本文介绍了如何在.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"
  },


推荐答案

project.json 中的> dependencies 属性指定程序包依赖关系,因此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.

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

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


  1. 放入您的 NameOfDll.dll MyClassLibrary.xproj
  2. 附近的 \lib 目录中>打开 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 将包含在NuGet包中的路径 lib下OfNameOfDll.dll

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

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

    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. 创建 \lib \构建 \内容 \tools 目录位于 MyClassLibrary.nuspec 附近。

    4. 将所有本机dll复制到 \build 文件夹中。

    5. 将已复制本机dll的扩展名更改为 .native

    6. 创建e MyClassLibrary.targets \build 文件夹中具有以下内容:

    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, \build, \content and \tools directories near MyClassLibrary.nuspec.
    4. Copy all your native dlls into the \build folder.
    5. Change extension of copied native dlls to .native.
    6. Create MyClassLibrary.targets with the following content inside \build 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

    <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天全站免登陆