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"
},
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)]
dependencies attribute in
project.json
specifies package dependencies, because of this NuGet handlesNameOfDll.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 nearMyClassLibrary.xproj
Open
project.json
file and add there:Execute
dotnet pack
NameOfDll.dll
will be included into NuGet package under the pathlib\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 nearMyClassLibrary.nuspec
.\build
folder..native
.Create
MyClassLibrary.targets
with the following content inside\build
folder: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
:Execute
nuget pack MyClassLibrary.csproj
to build your NuGet package.