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:
An unhandled exception of type 'System.DllNotFoundException' occurred in "NameOfDll.dll"
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"
},
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.
To add native C++ dlls into you NuGet package for .xproj library you should do the following steps:
NameOfDll.dll in \lib directory near MyClassLibrary.xproj
Open project.json file and add there:
"packOptions": {
"files" : {
"includeFiles" : "lib/NameOfDll.dll"
}
}
Execute dotnet pack
NameOfDll.dll will be included into NuGet package under the path lib\NameOfDll.dll.
To add native C++ dlls into your NuGet package for .csproj library you should do the following steps:
MyClassLibrary.csproj.nuget spec command.\lib, \build, \content and \tools directories near MyClassLibrary.nuspec.\build folder..native.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>
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.
Add the following lines into your MyClassLibrary.nuspec:
<files>
<file src="lib\" target="lib" />
<file src="tools\" target="tools" />
<file src="content\" target="content" />
<file src="build\" target="build" />
</files>
Execute nuget pack MyClassLibrary.csproj to build your NuGet package.
Here is a one-click way to do it in .net core 2. I used it in my website and it works.
Right click your project in Solution Explorer, select Add->Existing Item, browse and choose your dll (select show all extensions). Then your dll will appear in Solution explorer.
In the Solution Explorer, right-click your dll -> Properties. Set build action: Content, and Copy to Output Directory: Copy if newer (or always copy).
It's done. Use your dll in your code staightforward like this:
[DllImport("my_dll.dll", CallingConvention = CallingConvention.Cdecl)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With