【问题标题】:Copy C++ dlls to build output of any C# project that reference them by extension复制 C++ dll 以构建通过扩展引用它们的任何 C# 项目的输出
【发布时间】:2018-02-22 03:43:07
【问题描述】:
我有一个 C# 库,它引用了一些 C++\CLI 项目,其中包含一些编译为 dlls 的 C++ 库。
总而言之,在使用我的库的任何应用程序的运行时,我当然需要C# 和CLI dlls,但也需要所有C++,即使默认情况下也不会复制尽管它们被 CLI 项目引用。
使用预构建事件手动复制它们是可行的。
但我不仅需要为每个应用程序项目添加它们,而且我还想将我的库发布为NuGet 并且无法控制引用项目的属性。
有什么解决方法吗?特定于NuGet 包的事件一。
【问题讨论】:
标签:
c#
c++
dll
c++-cli
nuget-package
【解决方案1】:
我喜欢 Hans Passants cmets。他的回答减少了复制的数据量,并允许无缝使用 AnyCPU。
- 我的解决方案是由 WPF 项目使用的。支持旧项目文件的项目类型,nuget 不太支持作为 .NET Core 或 .NET Standard 项目。但是 .NET Standard 中没有 c++cli。我们只有 x64 程序集,我的消费者只是内部的,所以不需要关心 win32。
新的 nuget 语法允许对这些 dll 进行大量精细控制,但实际上我的学习曲线和 nuget 还没有完成。
所以这就是我用于严格包装 dll 的方法。那个人可以调试我的dll,我把调试的dll,pdbs放到包里,只在我们公司内部使用。如果用户将配置更改为调试,我会为这些程序集引用的包编写目标文件。
native dll
' 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>
'目标\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>