复制 C++ dll 以构建通过扩展引用它们的任何 C# 项目的输出

Copy C++ dlls to build output of any C# project that reference them by extension

我有一个 C# 库引用了一些 C++\CLI 项目,其中包含一些编译为 dllsC++ 库。

所以总而言之,在任何使用我的库的应用程序运行时,我当然需要 C#CLI dlls,但也需要所有 C++,即使它们被 CLI 项目引用,默认情况下也不会复制。

使用预构建事件手动复制它们是可行的。

但我不仅需要为每个应用程序项目添加它们,而且我还想将我的库发布为 NuGet,然后无法控制引用项目的属性。

有什么解决方法吗?特定于 NuGet 个包的事件一。

我喜欢 Hans Passants 的评论。他的回答减少了复制的数据量并允许无缝使用 AnyCPU。

  • 我的解决方案是 WPF 项目使用的。支持旧项目文件的项目类型,nuget 不太支持将其作为 .NET Core 或 .NET Standard 项目。但 .NET Standard 中没有 c++cli。我们只有 x64 程序集,我的消费者只是内部的,所以不需要关心 win32。 新的 nuget 语法允许对这些 dll 进行大量精细控制,但实际上我的学习曲线和 nuget 还没有完成。

这就是我用于严格包装 dll 的内容。人家可以debug进我的dll,我把debug的dll,pdbs打包进去,只在我们公司内部使用。如果用户将配置更改为调试,我会为这些程序集所引用的包编写目标文件。

本机 dll <-- c++cli 包装器 <-- c# 方便加载

' nuspec

<?xml version="1.0"?>
<package xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
    <description></description>
    <id>yourId</id>
    <version>$version$</version>
    <authors></authors>    
    <!-- Only done because at the beginning I copied the native dll into the lib/net461 folder which gets automatically referenced -->
    <references>
      <reference file="yourCSharpProject.dll" />
      <reference file="yourInteropProject.dll" /> <!-- if needed -->
    </references>
  </metadata>
  <files>
    <!-- the target file will copy my native dlls into the target file  -->
    <file src="target\**" target="build"/>

    <!-- copy all debug and release of ... Native.dlls, pdbs, xml -->
    <!-- usage hint ** preserves the folder structure, debug and release  -->
    <file src="bin\x64\**\YourNative.*" target="lib\native\" />  


    <!-- copy all debug and release of ... Interop.lls, pdbs, xml -->
    <file src="bin\x64\Release\.*" target="lib\net461\" />  
  </files>
</package>

' target\yourpackageName.target

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="CopyBinaries" BeforeTargets="BeforeBuild">
    <CreateItem Include="$(MSBuildThisFileDirectory)../lib/native/$(Configuration)/*" >
      <Output TaskParameter="Include" ItemName="SomeImportantName" />
    </CreateItem>
    <Copy SourceFiles="@(SomeImportantName)" 
          DestinationFolder="$(Outputpath)" 
          SkipUnchangedFiles="true" />
  </Target>
</Project>