如何在 .NET Core 项目中添加 C++ 库 [英] How to add C++ library in a .NET Core project
问题描述
如何在.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"
},
推荐答案
dependencies 属性在 project.json
中指定包依赖,因为这个 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.
要将本机 C++ dll 添加到 .xproj
库的 NuGet 包中,您应该执行以下步骤:
To add native C++ dlls into you NuGet package for .xproj
library you should do the following steps:
- 将您的
NameOfDll.dll
放在lib
目录中,靠近MyClassLibrary.xproj
打开
project.json
文件并在那里添加:
- Put your
NameOfDll.dll
inlib
directory nearMyClassLibrary.xproj
Open
project.json
file and add there:
"packOptions": {
"files" : {
"includeFiles" : "lib/NameOfDll.dll"
}
}
执行dotnet pack
NameOfDll.dll
将包含在路径 libNameOfDll.dll
下的 NuGet 包中.
NameOfDll.dll
will be included into NuGet package under the path libNameOfDll.dll
.
要将本机 C++ dll 添加到 .csproj
库的 NuGet 包中,您应该执行以下步骤:
To add native C++ dlls into your NuGet package for .csproj
library you should do the following steps:
- 我假设您已经管理了名为
MyClassLibrary.csproj
的项目. - 使用
nuget spec
命令为您的类库创建新的 NuGet 包. - 在
MyClassLibrary 附近创建
.lib
、uild
、content
和ools
目录.nuspec - 将所有本机 dll 复制到
uild
文件夹中. - 将复制的本机 dll 的扩展名更改为
.native
. 在
uild
文件夹中使用以下内容创建MyClassLibrary.targets
:
- I assume you have managed project with name
MyClassLibrary.csproj
. - Create new NuGet package for you class library with
nuget spec
command. - Create
lib
,uild
,content
andools
directories nearMyClassLibrary.nuspec
. - Copy all your native dlls into the
uild
folder. - Change extension of copied native dlls to
.native
. Create
MyClassLibrary.targets
with the following content insideuild
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
中:
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>
执行 nuget pack MyClassLibrary.csproj
以构建您的 NuGet 包.
Execute nuget pack MyClassLibrary.csproj
to build your NuGet package.
这篇关于如何在 .NET Core 项目中添加 C++ 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!