如何在.NET Core项目中添加C ++库

时间:2016-09-07 19:21:10

标签: c++ dll .net-core

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

An unhandled exception of type 'System.DllNotFoundException' occurred in "NameOfDll.dll"

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

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

2 个答案:

答案 0 :(得分:10)

project.json中的

dependencies 属性指定了包依赖关系,因为此NuGet将NameOfDll.dll作为包ID处理,而不是作为dll名称。

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

  1. NameOfDll.dll放在\lib
  2. 附近的MyClassLibrary.xproj目录中
  3. 打开project.json文件并添加:

    "packOptions": {
      "files" : {
        "includeFiles" : "lib/NameOfDll.dll"
      }
    }
    
  4. 执行dotnet pack

  5. NameOfDll.dll将包含在路径lib\NameOfDll.dll下的NuGet包中。

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

    1. 我假设您管理的项目名称为MyClassLibrary.csproj
    2. 使用nuget spec命令为您的类库创建新的NuGet包。
    3. \lib附近创建\build\content\toolsMyClassLibrary.nuspec目录。
    4. 将所有原生dll复制到\build文件夹。
    5. 将复制的原生dll的扩展名更改为.native
    6. 使用MyClassLibrary.targets文件夹中的以下内容创建\build

      <?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内容取自this个问题。 上述.targets文件将在NuGet包的安装中注入到目标项目文件中。

    7. 将以下几行添加到MyClassLibrary.nuspec

      <files>
        <file src="lib\" target="lib" />
        <file src="tools\" target="tools" />
        <file src="content\" target="content" />
        <file src="build\" target="build" />
      </files>
      
    8. 执行nuget pack MyClassLibrary.csproj以构建您的NuGet包。

答案 1 :(得分:0)

这是在.net核心2中单击一次的方法。我在我的网站上使用它并且它有效。

在解决方案资源管理器中右键单击您的项目,选择添加 - >现有项目,浏览并选择您的dll(选择显示所有扩展名)。然后你的dll将出现在Solution explorer中。

在解决方案资源管理器中,右键单击您的dll - &gt;属性。设置构建操作:内容和复制到输出目录:如果更新则复制(或始终复制)。

已经完成了。在你的代码中使用你的dll就像这样:

[DllImport(“my_dll.dll”,CallingConvention = CallingConvention.Cdecl)]

相关问题