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. Part of content of .csproj of .Net Maui sample application:
<ItemGroup>
<!-- other declarations -->
<NativeReference Include="Lib/libadd.a">
<Kind>Static</Kind>
<ForceLoad>True</ForceLoad>
<IsCxx>True</IsCxx>
</NativeReference>
</ItemGroup>
Inside of .cs file, I am accessing the API which is from c++ library as is shown below:
[DllImport("__Internal", EntryPoint = "add")]
public static extern int add(int a, int b);
I am getting these 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 https://aka.ms/msbuild/MSB4803 for further details. [/Users/sadeesh.kumar/Desktop/Demo-rk/HelloWordApp/HelloWordApp.csproj::TargetFramework=net8.0-android]
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk.net8.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.Sdk.net8.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.Sdk.net8.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]
I followed links referred below but still having the reported issue:
1.https://stackoverflow.com/questions/75126187/dllimport-with-a-file-for-ios-in-maui
2.https://learn.microsoft.com/en-us/previous-versions/xamarin/cross-platform/cpp/#stage-2-wrapping-the-native-libraries-with-a-visual-studio-solution
Environment:
Visual studio code: Version: 1.96.2
MacOS: 14.7.1
XCode: 16.2
DotNet-SDK: 8.0.404