为C++/C Visual Studio解决方案项目创建Nuget包

Create Nuget package for C++/C Visual Studio Solution projects(为C++/C Visual Studio解决方案项目创建Nuget包)

本文介绍了为C++/C Visual Studio解决方案项目创建Nuget包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个用C/C++编写的遗留组件,我想把它们包装到Nuget包中,并从C#代码使用这个包。 在Nuget包中实际包装C++代码的最佳方式是什么? 我查看了CoApp解决方案,但它现在似乎不受支持(脱机稳定发布)。

推荐答案

我可以回答您如何将C++打包为来自Visual Studio 2017/2019的Nuget包。

这是我在Visual Studio中用来将zlib C++库打包为Nuget包的工具。我广泛使用了内置到Visual Studio中的T4模板来生成我们需要的文件。

这表明您希望将x86/Release/DEBUG和x64/Release/DEBUG打包到同一个包中。您需要构建所有目标,然后构建我们在此处创建的新项目以生成Nuget包。

当您在自己的代码中包含生成的Nuget包时,它将自动使用Includes和lib,这是它的零配置。您需要更改这些部件的属性和目标文件以匹配您的配置才能实现此操作。

由于大多数Nuget打包说明涵盖C#或C++Unix,因此这些信息很难计算和组合在一起。我不得不想出秘方。

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

  2. 您需要创建并添加下面的文件。

  3. 添加生成事件

  4. 修补对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	hirdparty";       // 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 Studio2019ProfessionalCommon7IDETextTransform.exe" -out %3 %2

rem uncomment pause to see output to help debug 
rem pause

exit 0

将其放入名为Third-Party-zlib_nusspec.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="..
ugetlibx64Debug*"    target="lib
ativex64Debug"     />
        <file src="..
ugetlibx64Release*"  target="lib
ativex64Release"     />
        
        <file src="..
ugetlibx86Debug*"    target="lib
ativex86Debug"     />
        <file src="..
ugetlibx86Release*"  target="lib
ativex86Release"     />

        <file src="..
ugetlibx86Debug*"    target="lib
ativewin32Debug"     />
        <file src="..
ugetlibx86Release*"  target="lib
ativewin32Release"     />

        <file src="........zlib.h"          target="build
ativeinclude" />
        <file src="........zconf.h"         target="build
ativeinclude" />

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

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

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

将其放在名为Third-Party-zlib_pros.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
ative$(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>

将其放入名为Third-Party-zlib_Target s.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
uget-4.6.2
uget.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
uget-4.6.2
uget.exe pack <#= NugetShortname #>_nuspec.nuspec -OutputDirectory <#= NugetOutDir #>

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

exit

调用这些c++库函数可能会在其他地方得到回答。寻找pInvoke作为起点。

这篇关于为C++/C Visual Studio解决方案项目创建Nuget包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为C++/C Visual Studio解决方案项目创建Nuget包

基础教程推荐