linux – Persuading a Visual Studio C# project to copy native libraries to build directory


I’ve written a .NET7 library that uses P/Invoke to exercise supporting functions in two third party libraries, in this case .so files since the consuming product will only be deployed to Linux.

I’ve successfully packaged those files into a Nuget file, using the following project definition:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <RuntimeIdentifier>linux-x64</RuntimeIdentifier>
    <PackageId>my.library.name</PackageId>
    <Title>My Library</Title>
    <Authors>Me</Authors>
    <Company>My Company</Company>
    <Product>The Library</Product>
  </PropertyGroup>

  <ItemGroup>
    <Content Include="../res/liblib1.so" Pack="true" PackagePath="contentFiles/any/any">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      <PackageCopyToOutput>True</PackageCopyToOutput>
    </Content>
    <Content Include="../res/liblib2.so" Pack="true" PackagePath="contentFiles/any/any">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      <PackageCopyToOutput>True</PackageCopyToOutput>
    </Content>
  </ItemGroup>

</Project>

However, when the consuming project is built and published, the .so files are nowhere to be seen. The solution detailed in this answer didn’t get me anywhere, and the PackagePath as defined in this project was the result of reading a learn.microsoft.com article on cross-version compatibility (notwithstanding that both projects are using .NET7).

I expected the .so files to appear adjacent to the executable and its associated DLLs, or, worst case, in a subfolder so I would have to tweak the buildscript to reposition them. Instead, they aren’t copied at all.

I have confirmed by unpacking the nuget package that the files are present, in contentfiles/any/any, as expected from the PackagePath element, but they are not being included in the build products of the consuming project.

(I am using Visual Studio 2022 as IDE and compiler.)

Leave a Reply

Your email address will not be published. Required fields are marked *