【问题标题】:How to add C++ library in a .NET Core project如何在 .NET Core 项目中添加 C++ 库
【发布时间】:2017-12-12 19:08:27
【问题描述】:

在 .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"
  },

【问题讨论】:

    标签: c++ dll .net-core


    【解决方案1】:
    project.json 中的

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

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

    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 添加到您的 NuGet 包中以用于 .csproj 库,您应该执行以下步骤:

    1. 我假设您管理的项目名称为 MyClassLibrary.csproj
    2. 使用nuget spec 命令为您的类库创建新的 NuGet 包。
    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 包。

    【讨论】:

    • 嗨@gemr1423!它能解决您的问题还是您需要更多帮助?
    • 嗨@Nikita 我有一个问题,.xproj 也一样吗?
    • @gemr1423 添加了 xproj 的原生 dll 打包信息。
    • 嗨@Nikita,我在尝试在类库项目中执行C++ DLL 的方法时遇到了同样的错误。我完成了您解释的步骤,没有错误。 “无法加载 DLL 'TLHDArchive.dll':找不到指定的模块。(来自 HRESULT 的异常:0x8007007E)”我这样调用该方法:[DllImport("NameOfDll.dll", CallingConvention = CallingConvention.Cdecl)]
    • 感谢您的帮助@Nikita
    【解决方案2】:

    这是在 .net core 2 中执行此操作的一键式方法。我在我的网站中使用了它,它可以工作。

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      • 2016-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多