CPack NuGet软件包 [英] CPack NuGet Packages

查看:86
本文介绍了CPack NuGet软件包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Managed C ++/CLR库,它使用CMake 3.17构建,并使用CPack打包到NuGet包中.由于软件包管理器发出以下错误,因此无法将生成的nupkg文件导入C#项目:"[snip]软件包不包含任何与[.NETFramework,Version = v4.5.2]兼容的程序集引用或内容文件.".但是,添加对项目的引用(添加到解决方案中时)或由构建生成的相应库文件,可以按预期工作.

I have a Managed C++/CLR library which is built using CMake 3.17, and packaged into a NuGet package using CPack. The resulting nupkg file cannot be imported into a C# project, as the Package Manager issues the following error: "[snip] the package does not contain any assembly references or content files that are compatible with [.NETFramework,Version=v4.5.2]". However, adding a reference to either the project when added to the solution, or the corresponding library file generated by the build, works as intended.

代码本身是非常基本的,并且会生成一个有效的库,当通过Visual Studio 2017手动添加引用时,可以从另一个项目中引用它->添加引用(可以通过这种方式添加项目或相应的库,都一样).

The code itself is very basic and produces a valid library which can be referenced from another project, when manually adding a reference via Visual Studio 2017 -> Add Reference (either the project or the corresponding library can be added this way and it works all the same).

代码由类本身组成,而AssemblyInfo.cpp提供的属性仅描述元数据和版本信息.依赖项仅包括System,System :: Runtime :: InteropServices和大量预构建的本机库.

The code consists of the class itself, and AssemblyInfo.cpp provides attributes which describe the metadata and version information only. The dependencies include only System, System::Runtime::InteropServices, and a raft of pre-built native libraries.

我没有添加.nuspec文件,也没有添加nuget.config文件,我相信后者是由CPack NuGet生成器在构建程序包时生成的.

I have not added a .nuspec file, nor a nuget.config file, the latter which I believe is generated by the CPack NuGet generator when the package is built.

CPack NuGet支持是相对较新的,并且我一直未能找到有效的示例,但是我设法成功生成了一个nupkg文件.首先,指示CMake使用包含的源文件构建托管C ++库,并在相应的目标 ManagedLibrary 上设置以下属性:

CPack NuGet support is relatively new, and I have been unsuccessful in finding a working example, but I have managed to successfully generate a nupkg file. Firstly CMake is instructed to build a Managed C++ library with the included source files, and the following properties set on the corresponding target ManagedLibrary:

set_target_properties (ManagedLibrary PROPERTIES DOTNET_TARGET_FRAMEWORK_VERSION "v4.5.2")
set_target_properties (ManagedLibrary PROPERTIES COMMON_LANGUAGE_RUNTIME "")

文档指出,这将生成CLR/混合代码并按广告进行工作,因此我能够针对目标框架成功构建.下一步是将库安装在我认为是正确的位置:

The documentation states that this will generate CLR/Mixed code and works as advertised, so I am able to successfully build against the target framework. The next step was to install the library in what I believe is the correct location:

install (TARGET ManagedLibrary DESTINATION . COMPONENT MixedCLR)

并且类似地安装了支持(本机C ++)库:

And supporting (native C++) libraries are installed similarly:

install (FILES [various..] DESTINATION . COMPONENT MixedCLR)

我还将CPACK_GENERATOR设置为'NuGet',然后使用 cmake --build从CLI运行PACKAGE步骤.--target PACKAGE 会成功生成nupkg文件.

I also set CPACK_GENERATOR to 'NuGet', and then run the PACKAGE step from the CLI using cmake --build . --target PACKAGE which successfully produces the nupkg file.

NuGet如何知道要添加引用的库?

How does NuGet know what libraries to add a reference to?

  1. 是否需要nuspec文件?如果是这样,则必须至少包含哪些内容,以及如何将其包含在目标CMakeLists.txt中?
  2. 是否可以将托管库以及支持的本机库放在程序包的根目录中?如果没有,他们应该去哪里?
  3. nupkg文件中是否还包含其他文件?

最后,如果有人对使用C ++/CLR进行打包和多目标化以支持不同的框架版本/体系结构/构建配置一无所知,那么对此的任何说明将不胜感激.

Finally, if anyone knows anything about packaging and multi-targeting in C++/CLR to support different framework versions / architectures / build configurations, any notes on that would be highly appreciated.

推荐答案

NuGet如何知道要添加引用的库?

How does NuGet know what libraries to add a reference to?

从根本上讲,NuGet从程序包结构中推断出要引用的库.托管程序集必须放在 libs/< TFWM> 的目录中,其中 TFWM 是目标框架Moniker(例如:.NET Framework 4.5.2 => net452)

Primarily NuGet infers the libraries to reference from the package structure. Managed assemblies must be put in a directory which is libs/<TFWM> where TFWM is the Target Framework Moniker (eg: .NET Framework 4.5.2 => net452).

Is a nuspec file required? If so, what must minimally be included in it, and how do I include it in the target CMakeLists.txt?

nuspec文件由CPack在软件包生成时自动生成.生成的文件将保存到输出目录,并将保留 install 命令指定的目录结构.

The nuspec file is automatically generated by CPack at package generation time. The generated file is saved to the output directory, and will preserve the directory structure specified by the install command.

Is it acceptable to put the managed library, along with supporting native libraries, in the root of the package? If not, where should

他们去吗?

如前所述,托管库位于 libs/blah 中.另一方面,本机库进入 runtimes/< RID>/native ,其中 RID 是运行时ID.就我而言,我想定位到64位Windows,因此运行时ID为 win-x64 .

As already discussed, the managed libraries go in libs/blah. Native libraries, on the other hand, go in runtimes/<RID>/native where RID is the Runtime ID. In my case I wanted to target Windows 64-bit, so the Runtime ID is win-x64.

Are any other files generally included in a nupkg file?

为方便起见,我将PDB捆绑在一起,但不需要指定任何其他文件或属性.

I bundle the PDB for convenience, but I didn't need to specify any other files or properties.

最后,如果有人对包装和多目标化有所了解在C ++/CLR中支持不同的框架版本/体系结构/构建配置,对此的任何注释将不胜感激.

Finally, if anyone knows anything about packaging and multi-targeting in C++/CLR to support different framework versions / architectures / build configurations, any notes on that would be highly appreciated.

如果针对多个框架版本,这只是在多个文件夹中创建和安装多个目标的简单情况,那么处理起来就没有什么复杂的了.

If targeting multiple framework versions it's simply a case of creating and installing multiple targets into the respective folders, there's nothing more complex to deal with.

最后,我完成的包装结构如下:

Finally, my finished package structure looks like the following:

libs/
  net452/
    ManagedLib.dll
    ManagedLib.pdb
runtimes/
  win-x64/
    native/
      NativeLib1.dll
      NativeLib2.dll
      ...

我希望这对以后的人有所帮助.

I hope this helps someone in the future.

这篇关于CPack NuGet软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆