从非托管(C ++/C)代码创建Nuget程序包 [英] Create Nuget package from unmanaged (C++/C) code

查看:56
本文介绍了从非托管(C ++/C)代码创建Nuget程序包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个用C/C ++编写的遗留组件,想将它们包装到Nuget包中,并使用C#代码中的此包.将C ++代码实际包装在nuget包中的最佳方法是什么?我查看了 CoApp 解决方案,但是现在似乎不支持该解决方案(离线稳定发布)./p>

I have several legacy components written in C/C++ and would like to wrap them into the Nuget package and use this package from C# code. What is the best way to actually wrap C++ code in nuget package? I had a look on CoApp solution, but it seems like unsupported now (stable release offline).

推荐答案

我可以回答如何从Visual Studio 2017/2019将c ++打包为nuget包.

这就是我用来将zlib C ++库打包为Visual Studio中的nuget包的原因.我大量使用了Visual Studio内置的T4模板来生成所需的文件.

This is what I used to package up a zlib C++ library as a nuget package in visual studio. I make extensive use of T4 templates built into Visual Studio to generate the files we need.

这假定您希望将x86/release/debug和x64/release/debug打包在同一软件包中.您需要构建所有目标,然后构建我们在此处创建的新项目以生成nuget包.

This asumes you wish to package x86/release/debug and x64/release/debug in the same package. You need to build all the targets then build the new project that we create here to generate the nuget packages.

当您在自己的代码中包含生成的nuget程序包时,它将自动使用include和lib(其零配置).您需要更改这些零件属性和目标文件以匹配您的配置,以实现此目的.

When you include the resultant nuget package in your own code it will automatically use includes and lib, its zero config. You will need to change these parts prop and target file to match your config for this to happen.

由于大多数关于nuget的打包说明都涵盖了c#或C ++ Unix,因此很难对这些信息进行汇总.我必须找出秘密的调味料.

This information was quite hard to workout and put together as most nuget packaing instructions cover c# or C++ Unix. I had to work out the secret sauce.

  1. 在Visual Studio中为基本的c#应用程序创建新项目.

  1. Create new project for a basic c# app in visual studio.

您需要在下面创建并添加文件.

You need to creat and add the files below.

添加构建事件

修补对nuget.exe之类的外部引用.

Patch up extenal references to things like nuget.exe.

将此文件放在名为thirdparty.ttinc

<#@ assembly name="System" 
#><#@ assembly name="System.Core" 
#><#@ import namespace="System.IO" 
#><#@ import namespace="System.Linq" 
#><#@ import namespace="System.Text" 
#><#@ import namespace="System.Configuration" 
#><#@ import namespace="System.Collections.Generic" 
#><#
    string DirRoot      = @"c:\work\thirdparty\";       // Git source code package installed at.
    string NugetPrefix  = "thirdparty";         // base name for 3rd party packages.
#>

将其放在名为_NugetVersionInclude.ttinc

<#@ include file= "thirdparty.ttinc" #><#
    // Configuration
    // increment this number for each internal release  

    string NugetInternalVersion      = @"1.0.2";                // Internal version number.

    // configured once for each new thirdparty set of code

    string NugetName                 = @"zlib";                 // Nuget name for this package.
    string NugetExternalVersion      = @"1_2_11";               // External version number as used by the original author.

    // you probably dont need to change below.

    string NugetShortname            = NugetPrefix + "-" + NugetName;   
    string NugetRootname             = NugetShortname + "_" + NugetExternalVersion; 
    string NugetFullname             = NugetRootname + "." + NugetInternalVersion;
    string NugetDescription          = NugetPrefix + " " + NugetName + " " + NugetExternalVersion;  
    string NugetOutDir               = "nuget\\";

    string DirSolution = Environment.GetEnvironmentVariable("SOLUTIONDIR");
    
#>

将此添加到PreBuildEvent

echo on
start NuGetPackagePack.bat 
exit

将此添加到PostBuildEvent

echo on
start _UpdateTemplate.bat . "thirdparty-zlib_nuspec.tt" "thirdparty-xlw_nuspec.nuspec"

start _UpdateTemplate.bat . "thirdparty-zlib_props.tt" "thirdparty-xlw_props.props"

start _UpdateTemplate.bat . "thirdparty-zlib_targets.tt" "thirdparty-xlw_targets.targets"

start _UpdateTemplate.bat . "NugetPackagePack.tt" "NugetPackagePack.bat"

start _UpdateTemplate.bat . "NugetPackagePush.tt" "NugetPackagePush.bat"

exit 0

将其放在名为_UpdateTemplate.bat

echo on
del %3
set SOLUTIONDIR=%1
"C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\TextTransform.exe" -out %3 %2

rem uncomment pause to see output to help debug 
rem pause

exit 0

将此文件放在名为thirdparty-zlib_nuspec.tt的文件中

<#@ template debug="true" hostspecific="true" language="C#" #><#@ 
 include file="_NugetVersionInclude.ttinc" #><#@
 output extension=".nuspec" #><?xml version="1.0"?>
  <package >
    <metadata>
      <id><#= NugetRootname #></id>
      <version><#= NugetInternalVersion #></version>
      <description> <#= NugetDescription #> </description>
      <authors>Original Authors and Anthony Lambert</authors>
      <tags>Native, native</tags>
    </metadata>
    <files>
        <file src="..\nuget\lib\x64\Debug\*"    target="lib\native\x64\Debug"     />
        <file src="..\nuget\lib\x64\Release\*"  target="lib\native\x64\Release"     />
        
        <file src="..\nuget\lib\x86\Debug\*"    target="lib\native\x86\Debug"     />
        <file src="..\nuget\lib\x86\Release\*"  target="lib\native\x86\Release"     />

        <file src="..\nuget\lib\x86\Debug\*"    target="lib\native\win32\Debug"     />
        <file src="..\nuget\lib\x86\Release\*"  target="lib\native\win32\Release"     />

        <file src="..\..\..\..\zlib.h"          target="build\native\include" />
        <file src="..\..\..\..\zconf.h"         target="build\native\include" />

        <file src="..\..\..\..\doc\*"           target="doc"    />

        <file src="README.md"                target="" />

        <file src="<#= NugetShortname #>_props.props"       target="build\native\\<#= NugetRootname #>.props" />
        <file src="<#= NugetShortname #>_targets.targets"   target="build\native\\<#= NugetRootname #>.targets" />
    </files>
</package>

将此文件放在名为thirdparty-zlib_props.tt的文件中

<#@ template debug="true" hostspecific="true" language="C#" #><#@ 
 include file="_NugetVersionInclude.ttinc" #><#@
  output extension=".props" #><?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="15.0">
  <!-- lambea 2020-06-24 - the mdd and md sub dirs are not used at present. Not sure any of this is used, picked up from example code. -->
  <PropertyGroup>
    <LibraryType Condition="'$(Configuration)'=='Debug'">mdd</LibraryType>
    <LibraryType Condition="'$(Configuration)'=='Release'">md</LibraryType>
  </PropertyGroup>
  <ItemGroup>
      <FilamentLibs Include="$(MSBuildThisFileDirectory)\lib\native\$(PlatformTarget)\$(Configuration)\*.lib" />
  </ItemGroup>
  <PropertyGroup>
    <!-- Expland the items to a property -->
    <FilamentLibraries>@(FilamentLibs)</FilamentLibraries>
  </PropertyGroup>
  <ItemDefinitionGroup>
      <ClCompile>   <AdditionalIncludeDirectories>$(MSBuildThisFileDirectory)\include</AdditionalIncludeDirectories>
      </ClCompile>
      <Link>
        <AdditionalDependencies>$(FilamentLibraries);%(AdditionalDependencies)</AdditionalDependencies>
      </Link>
  </ItemDefinitionGroup>
</Project>

将此文件放在名为thirdparty-zlib_targets.tt的文件中

<?xml version="1.0" encoding="utf-8"?>

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="15.0" >
  <ItemDefinitionGroup>
    <ClCompile>
      <AdditionalIncludeDirectories>$(MSBuildThisFileDirectory)include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
    </ClCompile>
    <Link>
      <AdditionalLibraryDirectories>$(MSBuildThisFileDirectory)lib\$(Configuration)\$(PlatformName);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
      <AdditionalDependencies>zlibstat.lib; zlibwapi.lib; %(AdditionalDependencies) </AdditionalDependencies>
    </Link>
  </ItemDefinitionGroup>
</Project>

将其放在名为NugetPackagePack.tt的文件中

<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ include file="_NugetVersionInclude.ttinc" #>
<#@ output extension=".bat" #>

_tools\nuget-4.6.2\nuget.exe pack <#= NugetShortname #>_nuspec.nuspec -OutputDirectory <#= NugetOutDir #>

echo "run NugetPackagePush.bat to put on nuget server."
pause

exit

将其放在名为NugetPackagePush.tt的文件中

<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ include file="_NugetVersionInclude.ttinc" #>
<#@ output extension=".bat" #>

_tools\nuget-4.6.2\nuget.exe pack <#= NugetShortname #>_nuspec.nuspec -OutputDirectory <#= NugetOutDir #>

echo "run NugetPackagePush.bat to put on nuget server."
pause

exit

调用这些c lib函数可能在其他地方回答.寻找pinvoke作为起点.

Calling those c lib functions is probably answered else where. Look for pinvoke as a starting point.

这篇关于从非托管(C ++/C)代码创建Nuget程序包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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