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

How to add C++ library in a .NET Core project

如何在 .NET Core 项目中添加 C++ 库(Class 库)。我尝试创建一个 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"
  },

dependencies project.json 中的属性指定包依赖项,因为此 NuGet 将 NameOfDll.dll 作为包 ID 处理,而不是作为 dll 名称处理。

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

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

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

NameOfDll.dll 将包含在路径 lib\NameOfDll.dll.

下的 NuGet 包中

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

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

    <?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 包。

这是在 .net core 2 中的一键式方法。我在我的网站上使用它并且有效。

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

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

完成了。像这样在代码中直接使用你的 dll:

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