How to use a (.a) Library in a .NET Maui iOS project

Wael Bouchnak 31 Reputation points
2023-03-16T13:01:31.2733333+00:00

I was able to use a (.a) library from my xamarin project.

i used to add my library this way in the iOS project .csproj

  <ItemGroup>
    <NativeReference Include="Native References/MyLib.a">
      <Kind>Static</Kind>
      <ForceLoad>True</ForceLoad>
      <IsCxx>True</IsCxx>
      <SmartLink>True</SmartLink>
    </NativeReference>
  </ItemGroup>

in my Xamarin Project, every thing works fine :)


i tried to migrate the project to MAUI, I added the same functions, wrappers in my .NET Maui project... and i was not able to call the library

i tried first to add the same (.a) library in .csproj in this way ( Does not work )

	<ItemGroup>	  
    <NativeReference Include="Platforms\iOS\libs\MyLib.a">
      <Kind>Static</Kind>
      <ForceLoad>True</ForceLoad>
      <IsCxx>True</IsCxx>
      <SmartLink>True</SmartLink>
    </NativeReference>
  </ItemGroup>

then i tried this ( also does not work )

	<ItemGroup>	  
    <ObjBindingNativeLibrary Include="Platforms\iOS\libs\MyLib.a">
    </ObjBindingNativeLibrary>
  </ItemGroup>

i don't find any documentation about this in microsoft Maui docs

is there any project or any sample or any docs about this ???

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,837 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Stefan Gstrein 0 Reputation points
    2024-02-19T12:00:53.5666667+00:00

    This is my working solution:

    I have my library in the root of my iOS Platforms directory: /Platforms/iOS/libCLib.iOS.a Important in the iOS Build settings: Linker behaviour for Debug has to be "Don't link" or "Link Framework SDKs Only".

    <PropertyGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">
        <MtouchLink>Full</MtouchLink>
        <AdditionalArgs>-cxx -gcc_flags "-L${ProjectDir} -lCLib.iOS -force_load ${ProjectDir}/libCLib.iOS.a"</AdditionalArgs>
    </PropertyGroup>
    
    <ItemGroup>
        <NativeReference Include="Platforms\iOS\libCLib.iOS.a" Kind="Static">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </NativeReference>      
    </ItemGroup>
    
    0 comments No comments