xamarin - How to add static library(.a) in to .Net Maui iOS application - Stack Overflow

时间: 2025-01-06 admin 业界
I'm trying to add static library(.a) in to .Net Maui iOS application. I use functions from native binaries via DllImport attributes. So for example:


Inside .a file contains   
 // add.cpp extern "C" int add(int a, int b) { return a + b; }

After creating c++ project, I am converting into .a file format like below:
g++ -c add.cpp -o add.o
ar rcs libadd.a add.o

Adding libadd.a file in to .Net MAui Application like below:

.Net Maui sample application
inside .csproj

<NativeReference Include="Lib/libadd.a">
      <Kind>Static</Kind>
      <ForceLoad>True</ForceLoad>
      <IsCxx>True</IsCxx>
    </NativeReference>
  </ItemGroup>

inside .cs file i am accessing the API which is from c++ library as below:

 [DllImport("__Internal", EntryPoint = "add")]
 public static extern int add(int a, int b);
i am getting below errors/usr/local/share/dotnet/sdk/8.0.404/Microsoft.Common.CurrentVersion.targets(2303,5): error MSB4803: The task "ResolveNativeReference" is not supported on the .NET Core version of MSBuild. Please use the .NET Framework version of MSBuild. See  for further details. [/Users/sadeesh.kumar/Desktop/Demo-rk/HelloWordApp/HelloWordApp.csproj::TargetFramework=net8.0-android]
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk8.0_18.0/18.0.8319/targets/Xamarin.Shared.Sdk.targets(1648,3): error : clang++ exited with code 1: [/Users/sadeesh.kumar/Desktop/Demo-rk/HelloWordApp/HelloWordApp.csproj::TargetFramework=net8.0-ios]
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk8.0_18.0/18.0.8319/targets/Xamarin.Shared.Sdk.targets(1648,3): error : ld: building for 'iOS-simulator', but linking in object file (/Users/sadeesh.kumar/Desktop/Demo-rk/HelloWordApp/Platforms/iOS/Lib/libMathFuncs.a[arm64][2](MyMathFuncs.o)) built for 'iOS' [/Users/sadeesh.kumar/Desktop/Demo-rk/HelloWordApp/HelloWordApp.csproj::TargetFramework=net8.0-ios]
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk8.0_18.0/18.0.8319/targets/Xamarin.Shared.Sdk.targets(1648,3): error : clang++: error: linker command failed with exit code 1 (use -v to see invocation) [/Users/sadeesh.kumar/Desktop/Demo-rk/HelloWordApp/HelloWordApp.csproj::TargetFramework=net8.0-ios]

Referred below link but still i am facing error:

1.
2.

Environment:

  • Visual studio code: Version: 1.96.2

  • MacOS: 14.7.1

  • XCode: 16.2

  • DotNet-SDK: 8.0.404