2016-09-07 32 views
7

Cách thêm thư viện C++ vào dự án .NET Core (Thư viện lớp). Tôi đã thử tạo một gói nuget nhưng không hoạt động. Tôi đã nhận lỗi này:Cách thêm thư viện C++ vào dự án .NET Core

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

Khi tôi thêm các NuGet gói project.json thêm tài liệu tham khảo sau đây:

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

Trả lời

7

phụ thuộc thuộc tính trong project.json xác định gói phụ thuộc, vì NuGet này xử lý NameOfDll.dll làm ID gói, nhưng không phải là tên dll.

Để thêm bản ngữ C++ dlls vào bạn NuGet gói cho .xproj thư viện bạn nên làm theo các bước sau:

  1. Đặt thư mục NameOfDll.dll trong \lib ở gần MyClassLibrary.xproj
  2. mở project.json file và thêm có:

    "packOptions": { 
        "files" : { 
        "includeFiles" : "lib/NameOfDll.dll" 
        } 
    } 
    
  3. Thực hiện dotnet pack

NameOfDll.dll sẽ được đưa vào gói NuGet theo đường dẫn lib\NameOfDll.dll.

Để thêm bản C++ dlls vào gói NuGet cho .csproj thư viện bạn nên làm theo các bước sau:

  1. Tôi giả sử bạn đã quản lý dự án với tên MyClassLibrary.csproj.
  2. Tạo gói NuGet mới cho thư viện lớp học của bạn với lệnh nuget spec.
  3. Tạo \lib, \build, \content\tools thư mục gần MyClassLibrary.nuspec.
  4. Sao chép tất cả các tệp gốc của bạn vào thư mục \build.
  5. Thay đổi tiện ích mở rộng của các tệp gốc được sao chép thành .native.
  6. Tạo MyClassLibrary.targets với nội dung sau bên \build thư mục:

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

    Gợi ý: Nội dung .targets được lấy từ this câu hỏi. Tập tin .targets ở trên sẽ được tiêm khi cài đặt gói NuGet vào tệp dự án đích.

  7. Thêm các dòng sau vào MyClassLibrary.nuspec của bạn:

    <files> 
        <file src="lib\" target="lib" /> 
        <file src="tools\" target="tools" /> 
        <file src="content\" target="content" /> 
        <file src="build\" target="build" /> 
    </files> 
    
  8. Execute nuget pack MyClassLibrary.csproj để xây dựng gói NuGet của bạn.

+0

Hi @ gemr1423! Nó có giải quyết được vấn đề của bạn hay bạn cần thêm trợ giúp? – Nikita

+0

Hi @Nikita Tôi có một câu hỏi, giống với .xproj? – gemr1423

+0

@ gemr1423 Thông tin về việc đóng gói các tệp dll gốc với 'xproj' được thêm vào. – Nikita

0

Đây là cách thực hiện một cú nhấp chuột trong lõi .net 2. Tôi đã sử dụng nó trong trang web của mình và nó hoạt động.

Nhấp chuột phải vào dự án của bạn trong Solution Explorer, chọn Thêm>> Mục hiện có, duyệt và chọn dll của bạn (chọn hiển thị tất cả tiện ích mở rộng). Sau đó, dll của bạn sẽ xuất hiện trong Solution explorer.

Trong Solution Explorer, nhấp chuột phải vào dll của bạn -> Thuộc tính. Đặt hành động xây dựng: Nội dung và Sao chép vào Thư mục Đầu ra: Sao chép nếu mới hơn (hoặc luôn sao chép).

Hoàn tất. Sử dụng dll của bạn trong staightforward mã của bạn như thế này:

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

Các vấn đề liên quan