MsBuild copies wrong dll (x86 to x64) to build folder

Hi. focusing problem with usage of native library. What im trying to achive: x-platform library (nuget) that uses native dlls specified by target platform.
12 Replies
~FallenParadise~
~FallenParadise~OP2w ago
What i have is:
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<PackageId>LuaDll</PackageId>
<Authors></Authors>
<Description></Description>
<PackageLicenseExpression></PackageLicenseExpression>
<PackageProjectUrl></PackageProjectUrl>
<RepositoryUrl></RepositoryUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
</PropertyGroup>

<ItemGroup>
<!-- Windows x64 -->
<NativeLibrary Include="lib\win-x64\lua54.dll">
<TargetPlatformIdentifier>Windows</TargetPlatformIdentifier>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
</NativeLibrary>

<!-- Windows x86 -->
<NativeLibrary Include="lib\win-x86\lua54.dll">
<TargetPlatformIdentifier>Windows</TargetPlatformIdentifier>
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
</NativeLibrary>

<!-- Linux x64 -->
<NativeLibrary Include="lib\linux-x64\lua54.so">
<TargetPlatformIdentifier>Linux</TargetPlatformIdentifier>
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
</NativeLibrary>

<!-- Linux x86 -->
<NativeLibrary Include="lib\linux-x86\lua54.so">
<TargetPlatformIdentifier>Linux</TargetPlatformIdentifier>
<RuntimeIdentifier>linux-x86</RuntimeIdentifier>
</NativeLibrary>
</ItemGroup>

<ItemGroup>
<Folder Include="lib\" />
<Folder Include="Properties\" />
</ItemGroup>


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

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<PackageId>LuaDll</PackageId>
<Authors></Authors>
<Description></Description>
<PackageLicenseExpression></PackageLicenseExpression>
<PackageProjectUrl></PackageProjectUrl>
<RepositoryUrl></RepositoryUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
</PropertyGroup>

<ItemGroup>
<!-- Windows x64 -->
<NativeLibrary Include="lib\win-x64\lua54.dll">
<TargetPlatformIdentifier>Windows</TargetPlatformIdentifier>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
</NativeLibrary>

<!-- Windows x86 -->
<NativeLibrary Include="lib\win-x86\lua54.dll">
<TargetPlatformIdentifier>Windows</TargetPlatformIdentifier>
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
</NativeLibrary>

<!-- Linux x64 -->
<NativeLibrary Include="lib\linux-x64\lua54.so">
<TargetPlatformIdentifier>Linux</TargetPlatformIdentifier>
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
</NativeLibrary>

<!-- Linux x86 -->
<NativeLibrary Include="lib\linux-x86\lua54.so">
<TargetPlatformIdentifier>Linux</TargetPlatformIdentifier>
<RuntimeIdentifier>linux-x86</RuntimeIdentifier>
</NativeLibrary>
</ItemGroup>

<ItemGroup>
<Folder Include="lib\" />
<Folder Include="Properties\" />
</ItemGroup>


</Project>
primary csproj. gpt says that it should work while it doesn't. When im using Sample x86 all works fine. But if i switch to x64 i see x86 dll in x64 folder. And that's how Sample project looks like
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Platforms>AnyCPU;x86;x64</Platforms>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\LuaDll\LuaDll.csproj" />
</ItemGroup>

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

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Platforms>AnyCPU;x86;x64</Platforms>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\LuaDll\LuaDll.csproj" />
</ItemGroup>

</Project>
i can't understand how to properly configure MsBuild to copy correct Dll depending on target platform. im also sure that lib/[plat]/lua54.dll has correct versions of dlls ------------ What i want: LuaDLL - primary project with bindings to a lua Sample - PackageReference to LuaDLL to test apis (and then convert to xUnit project) LuaDLL.nuget - as crossplatform nuget. so it whould be enough just to import and use
Lex Li
Lex Li2w ago
Enable MSBuild bin log and see what confused the engine to use the wrong dll, https://msbuildlog.com/ Then depending on your findings, you either fix that issue in your script, or report a bug to Microsoft https://github.com/dotnet/msbuild/issues
wasabi
wasabi2w ago
For Core you place them in the runtimes folder. For Framework you need to write a targets file to copy them. For in tree consumption you have to do it in custom targets as well.
~FallenParadise~
~FallenParadise~OP2w ago
using net9 should i share logs with you?
Lex Li
Lex Li2w ago
Please learn to analyze the logs yourself. It's not very difficult. If you do need technical support to assist, you can open a support case via https://support.microsoft.com
Microsoft Support
Microsoft Support is here to help you with Microsoft products. Find how-to articles, videos, and training for Microsoft Copilot, Microsoft 365, Windows, Surface, and more.
~FallenParadise~
~FallenParadise~OP2w ago
seems like it just ignores RID in NativeLibrary definition when this project used with package reference it found 2 candidates (and because thay both is lua54.dll) it copies one over another ok. seems like this hack helped and it works more or less properly
<ItemGroup>
<ProjectReference Include="..\..\src\LuaDll\LuaDll.csproj">
<Properties>RuntimeIdentifier=$(RuntimeIdentifier);SelfContained=$(SelfContained)</Properties>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\LuaDll\LuaDll.csproj">
<Properties>RuntimeIdentifier=$(RuntimeIdentifier);SelfContained=$(SelfContained)</Properties>
</ProjectReference>
</ItemGroup>
in Sample.csproj thanks @Lex Li for pointing into logs lets summarize. LuaDll.csproj still uses NativeLibrary. aka
<None Include="lib\win-x64\lua54.dll">
<Pack>true</Pack>
<PackagePath>runtimes\win-x64\native\</PackagePath>
</None>
<None Include="lib\win-x86\lua54.dll">
<Pack>true</Pack>
<PackagePath>runtimes\win-x86\native\</PackagePath>
</None>
<None Include="lib\win-x64\lua54.dll">
<Pack>true</Pack>
<PackagePath>runtimes\win-x64\native\</PackagePath>
</None>
<None Include="lib\win-x86\lua54.dll">
<Pack>true</Pack>
<PackagePath>runtimes\win-x86\native\</PackagePath>
</None>
And Sample.csproj is just
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Platforms>AnyCPU;x86;x64</Platforms>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\LuaDll\LuaDll.csproj">
<Properties>RuntimeIdentifier=$(RuntimeIdentifier);SelfContained=$(SelfContained)</Properties>
</ProjectReference>
</ItemGroup>

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

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Platforms>AnyCPU;x86;x64</Platforms>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\LuaDll\LuaDll.csproj">
<Properties>RuntimeIdentifier=$(RuntimeIdentifier);SelfContained=$(SelfContained)</Properties>
</ProjectReference>
</ItemGroup>

</Project>
wasabi
wasabi2w ago
I don't know what NativeLibrary is.
Unknown User
Unknown User7d ago
Message Not Public
Sign In & Join Server To View
wasabi
wasabi7d ago
Mostly the platforms property isn't very useful.
Unknown User
Unknown User7d ago
Message Not Public
Sign In & Join Server To View
wasabi
wasabi7d ago
RuntimeIdentifier is really the replacement. It is, because it does drive out a bunch of things. But it's also set by RID. It is not necessary to use Platforms at all.
Unknown User
Unknown User7d ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?