Search code examples
c#c++dllc++-clinuget-package

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


I have a C# library that references some C++\CLI project that contains a few C++ libraries compiled as dlls.

So all in all, on runtime of any app that uses my library, I need of course the C# and CLI dlls, but also all the C++ ones, which are not copied by default even though they are referenced by the CLI project.

Using Pre-build events to copy them manually works.

But not only do I need to add them per application project, but I also want to publish my library as NuGet and cannot control the properties of the referenced projects then.

Any workaround? Event one that is specific to NuGet packages.


Solution

  • I like Hans Passants comments. His answer reduces the amount of copied data and allow the seamless use of AnyCPU.

    • My solution is something which is used by a WPF project. The project type supporting old project files, that are not quite supported by nuget as .NET Core or .NET Standard projects. But no c++cli in .NET Standard. We only have x64 assemblies and my consumers are only internal, so do not need to care about win32. The new nuget syntax allows a great deal in fine control for those dlls, but actually I'm not done with my learning curve and nuget.

    So this is what i use for a strict wrapping dll. That people can debug into my dll, I put the debug dll, pdbs into the package, they only are consumed within our company. I do write the target file for the packages that those assemblies get referenced if the user changes the configuration to debug.

    native dll <-- c++cli wrapper <-- c# convenience load

    ' 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>