问题描述
如何在.NET Core项目(类库)中添加C ++库。我尝试创建一个nuget包,但是没有用。我收到此错误:
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:
NameOfDll.dll中发生类型为 System.DllNotFoundException的未处理异常
当我添加nuget包时,project.json添加以下引用:
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"
},
推荐答案
project.json 中的> dependencies 属性指定程序包依赖关系,因此NuGet处理 NameOfDll.dll
作为程序包ID,而不是dll名称。
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.
要为 .xproj $>将本机C ++ dll添加到您的NuGet程序包中。 c $ c>库,您应该执行以下步骤:
To add native C++ dlls into you NuGet package for .xproj
library you should do the following steps:
- 放入您的
NameOfDll.dll
在MyClassLibrary.xproj
附近的
\lib
目录中>打开 project.json
文件并在其中添加:- Put your
NameOfDll.dll
in\lib
directory nearMyClassLibrary.xproj
Open
project.json
file and add there:
"packOptions": {
"files" : {
"includeFiles" : "lib/NameOfDll.dll"
}
}
执行 dotnet pack
NameOfDll.dll
将包含在NuGet包中的路径 lib下OfNameOfDll.dll
。
NameOfDll.dll
will be included into NuGet package under the path lib\NameOfDll.dll
.
要将本机C ++ dll添加到您的NuGet包中,价格为 .csproj
库,您应该执行以下步骤:
To add native C++ dlls into your NuGet package for .csproj
library you should do the following steps:
- 我假设您已管理名称为
MyClassLibrary.csproj的项目
。 - 使用
nuget spec
命令为您的类库创建新的NuGet包。 - 创建
\lib
,\构建
,\内容
和\tools
目录位于MyClassLibrary.nuspec
附近。 - 将所有本机dll复制到
\build
文件夹中。 - 将已复制本机dll的扩展名更改为
.native
。 -
创建e
MyClassLibrary.targets
在\build
文件夹中具有以下内容:
- I assume you have managed project with name
MyClassLibrary.csproj
. - Create new NuGet package for you class library with
nuget spec
command. - Create
\lib
,\build
,\content
and\tools
directories nearMyClassLibrary.nuspec
. - Copy all your native dlls into the
\build
folder. - Change extension of copied native dlls to
.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>
提示: .targets
内容取自此问题。
上面的 .targets
文件将在安装NuGet软件包时注入到目标项目文件中。
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.
将以下行添加到您的 MyClassLibrary.nuspec
:
<files>
<file src="lib\" target="lib" />
<file src="tools\" target="tools" />
<file src="content\" target="content" />
<file src="build\" target="build" />
</files>
执行 nuget pack MyClassLibrary.csproj
来构建您的NuGet软件包。
Execute nuget pack MyClassLibrary.csproj
to build your NuGet package.
这篇关于如何在.NET Core项目中添加C ++库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!