本文介绍了创建用于C ++ / CLI(混合)程序集的NuGet程序包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我创建了一个C ++ / CLI(混合)程序集,该程序集具有一些非托管C ++代码的托管包装器类。托管部分的目标是.NET 4.6.1,我得到了一个文件 entry.cpp ,其中包含以下内容:I have created a C++/CLI (mixed) assembly which has a managed wrapper class around some unmanaged C++ code. The managed part targets .NET 4.6.1, I got a file entry.cpp with just this line to do that:[assembly:System::Runtime::Versioning::TargetFrameworkAttribute(L".NETFramework,Version=v4.6.1", FrameworkDisplayName = L".NET Framework 4.6.1")];当我现在在.NET 4.6.1项目中手动包括编译后的程序集时,可以使用托管When I now manually include the compiled assembly in a .NET 4.6.1 project I can use the managed class as expected.该项目可以通过四种方式构建: x86 或 x64 作为 debug 或 release 版本。它没有托管的依赖项。This project can be build four ways: x86 or x64 as either debug or release build. It has no managed dependencies.现在,我想要一个(或如果需要多个)NuGet程序包,可以将其上传到Feed中,并在每个.NET中轻松使用包装程序集我想要的4.6.1兼容项目。 我该如何实现?Now I want one (or if required multiple) NuGet packages which I can upload to my feed and use the wrapper assembly easily in every .NET 4.6.1 compatible project I would like. How do I achieve this?到目前为止,我尝试了两种方法:So far I tried two approaches:首先,我创建了一个 .autopkg 文件,该文件根据此博客文章提供本机DLL的方法。该文件的文件部分如下所示:First, I created a .autopkg file which is according to this blog post the way to provide native DLLs. The files section of that file looks like this:files { // include: { *.h }; [x86,v120,release] { symbols: { ..\Release\*.pdb; } bin: { ..\Release\*.dll; } }; [x86,v120,debug] { symbols: { ..\Debug\*.pdb; } bin: { ..\Debug\*.dll; } };};此过程将生成三个 .nupkg 文件我可以将其上传到我的供稿中。但是,当我尝试将该程序包安装到.NET 4.6.1项目时,出现以下错误消息:This process results in three .nupkg files which I can upload to my feed. But when I try to install that package to a .NET 4.6.1 project I get this error message: 所以我重新考虑了使用托管程序集的方法来创建 .nupkg ,因为程序集具有我要从托管代码中使用的托管类。我创建了一个 .nuspec (使用 nuget spec )并提供了元数据。然后,我尝试创建这样的程序包:So I rethought if I should not use the way for managed assembly to create the .nupkg because the assembly has a managed class I want to use from managed code. I created a .nuspec (using nuget spec) and provided the metadata. Then I try to create my package like this:nuget pack MyCppCliWrapper.nuspec -Prop Configuration=Release -Prop Platform=x86 -Build但这会导致一个包,其中包含整个项目以及所有源文件和临时文件,像该文件夹的zip文件。But that results in a package which contains the whole project with all source files and temporary files, just like a zip file of that folder.显然,也缺少有关目标框架的元信息。Obviously there is also missing the meta information about targeted framework.当我尝试使用项目文件创建程序包时(例如与C#程序集一样),该操作也会失败:When I try to use the project file to create the package (like with C# assemblies) this fails too: C ++项目文件 .vcxproj ,似乎不受NuGet支持(我正在使用NuGet 3.5.0.1938命令行实用程序)。The C++ project files, .vcxproj, seem to be unsupported by NuGet (I am using the NuGet 3.5.0.1938 command line utility).我需要手动生成并在 .nuspec 的code> files 部分?如果是,他如何从这一行知道哪个DLL是针对哪个.NET Framework和平台的?Will I need to build manually and provide all files in the files section of the .nuspec? If yes, how would he know from this line which DLL is for which .NET framework plus platform?<file src="bin\**\*.dll" target="lib" /> 我相信Hans Passant是正确的,这只是一个 regular 托管nuget程序包,但打包程序不处理 .vcxproj 文件,因此我组成了自己的 .nuspec :I believe Hans Passant is right, this is just a regular managed nuget package but the packager does not handle the .vcxproj files so I made up my own .nuspec:<?xml version="1.0"?><package > <metadata> ... </metadata> <files> <file src="readme.txt" target="" /> <file src="bin\Win32\Release\*.dll" target="lib\net461" /> <file src="bin\Win32\Release\*.pdb" target="lib\net461" /> </files></package>以这种方式生成的程序包。The package generated this ways works.还有一个问题:这样,我必须要做两个软件包,一个用于32位,一个用于64位吗?或者是否可以将它们包含在一个软件包中(我希望这样做),并让耗时的项目使用一个或多个?另一个取决于目标体系结构(any-cpu主要是32位)吗?There is one question remaining: This way, do I have to do two packages, one for 32bit and one for 64bit - or is it possible to include them in one package (which I would prefer) and have the consuming project use one or another depending on the target architecture (any-cpu is mostly 32bit)?推荐答案我不知道这是否仍然有帮助您,但是我设法打包了x64和x86 C ++代码以及在AnyCPU上编译的C#包装器。I don't know if this could still help you, but I've managed to pack both x64 and x86 C++ code, and a C# wrapper that was compiled on AnyCPU.在我的C#项目中,我有两个平台: x86和 x64。On My C# project, I have two Platforms: "x86" and "x64".在我的Nuget文件夹中,我具有以下结构:On my Nuget folder, I have the following structure:\Project \Project.1.0.nuspec \build \x64 \*.dll \*.pdb \x86 \*.dll \*.pdb \Project.targets \lib \net452 \Wrapper.dll \Wrapper.pdb项目。 nuspec:Project.nuspec:<?xml version="1.0" encoding="utf-8"?><package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd"> <metadata> <id>Project</id> <version>1.0</version> <authors>nilsonneto</authors> <owners>nilsonneto</owners> <requireLicenseAcceptance>false</requireLicenseAcceptance> <description>Example.</description> <references> <reference file="Wrapper.dll" /> </references> </metadata> <files> <file src="build\Project.targets" target="build\Project.targets" /> <file src="build\x64\**" target="build\x64" /> <file src="build\x86\**" target="build\x86" /> <file src="lib\net452\Wrapper.dll" target="lib\net452\Wrapper.dll" /> <file src="lib\net452\Wrapper.pdb" target="lib\net452\Wrapper.pdb" /> </files></package> Project.targets:Project.targets:<?xml version="1.0" encoding="utf-8"?><Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemGroup> <NativeLibs Include="$(MSBuildThisFileDirectory)\$(Platform)\*.*" /> <Content Include="@(NativeLibs)"> <Link>%(RecursiveDir)%(FileName)%(Extension)</Link> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> </ItemGroup></Project>注意$(Platform),这是在Visual Studio上构建平台的名称放置,这就是为什么我将C ++ DLL分隔在与Visual Studio中平台名称相同的文件夹中的原因。Notice the $(Platform), which is where the name of the Platform being build on Visual Studio will be placed, which is why I separated the C++ DLLs in folders with the same name as the Platforms in Visual Studio.然后根据文档说明( https://docs.microsoft.com/zh-cn/nuget/create-packages/ native-packages ),所有本机DLL必须放置在\build目录中。And according to the documentation (https://docs.microsoft.com/en-us/nuget/create-packages/native-packages), all native DLLs have to be placed in the \build directory.因此,只需调整文件夹名称基于您在.NET项目和您的集合上支持的平台。So, just adjust the folder names based on the Platforms you support on the .NET project and your set. 这篇关于创建用于C ++ / CLI(混合)程序集的NuGet程序包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-03 00:06