xamarin/xamarin-macios

Unable to Xamarin.iOS binding project migration

Closed this issue · 7 comments

From @nk-alex on Tue, 10 Sep 2024 09:25:54 GMT

Description

I'm just following the official tutorial.

Unable to make my library work for MAUI. It was working for Xamarin.Forms.

Steps to Reproduce

  1. Create a new iOS Binding Library
  2. Add your native library (which was working as expected in Xamarin.Forms)
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0-ios</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>true</ImplicitUsings>
    <IsBindingProject>true</IsBindingProject>
    <Platforms>AnyCPU;ARM64</Platforms>
  </PropertyGroup>

  <ItemGroup>
    <ObjcBindingApiDefinition Include="ApiDefinition.cs" />
    <ObjcBindingCoreSource Include="StructsAndEnums.cs" />
  </ItemGroup>

	<ItemGroup>
		<NativeReference Include="opencv2.a">
			<Kind>Static</Kind>
			<ForceLoad>True</ForceLoad>
		</NativeReference>
	</ItemGroup>

	<ItemGroup>
		<NativeReference Include="libFaceDetector.a">
			<Kind>Static</Kind>
			<ForceLoad>True</ForceLoad>
		</NativeReference>
	</ItemGroup>
</Project>
  1. Copy ApiDefinition.cs and StructsAndEnums.cs from Xamarin.iOS binding library. In my case, StructsAndEnums.cs is empty. ApiDefinition.cs looks like this:
[BaseType (typeof (NSObject))]
interface FaceDetector
{
	[Export ("initWithFaceCascade:")]
	IntPtr Constructor (string filePath);

	[Export ("detectFaces:")]
	NSArray DetectFaces(UIImage image);
}

[BaseType(typeof(NSObject))]
interface Version
{
    [Export("getOpenCVVersion")]
    NSString GetOpenCVVersion();
}

[BaseType(typeof(NSObject))]
interface PerspectiveCorrection
{
    [Export("CorrectPerspective:")]
	void CorrectPerspective(string imagePath);
}

[BaseType(typeof(NSObject))]
interface ExtractObject
{
    [Export("Debug:")]
    NSString Debug(NSData debugimage);

    [Export("ExtractObjectFromByteArray:")]
	NSArray ExtractObjectFromByteArray(NSData image);

    [Export("ExtractObjectFromFilePath:")]
	NSArray ExtractObjectFromFilePath(string imagePath);
}
  1. Connect to mac for compilation. This is not in tutorial, but otherwise will always compile despite errors. I compile as Release, AnyCPU.
  2. Reference the generated .dll from my MAUI project:
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0-ios'">
	<Reference Include="OpenCV.iOS.Binding">
		<HintPath>Path-to-file.dll</HintPath>
	</Reference>
</ItemGroup>

  1. MAUI project doesn't compile:
Error (active)		clang++ exited with code 1:
Undefined symbols for architecture arm64:
  "_OBJC_CLASS_$_ExtractObject", referenced from:
      objc-class-ref in registrar.o
  "_OBJC_CLASS_$_FaceDetector", referenced from:
      objc-class-ref in registrar.o
  "_OBJC_CLASS_$_PerspectiveCorrection", referenced from:
      objc-class-ref in registrar.o
  "_OBJC_CLASS_$_Version", referenced from:
      objc-class-ref in registrar.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)	AppName(net8.0-ios)	C:\Program Files\dotnet\packs\Microsoft.iOS.Sdk.net8.0_17.5\17.5.8020\targets\Xamarin.Shared.Sdk.targets	1641	
Warning (active)	MT1308	Could not extract the native library 'libFaceDetector.a' from the assembly '/Users/nk-alex/Library/Caches/Xamarin/mtbs/builds/AppName/5116569e17934bd075613c53b49f34db242a1910b7295550a4f86d3d2279d0df/Path-to-file.dll', because it doesn't contain the resource 'libFaceDetector.a'.	

Warning (active)	MT1308	Could not extract the native library 'opencv2.a' from the assembly '/Users/nk-alex/Library/Caches/Xamarin/mtbs/builds/AppName/5116569e17934bd075613c53b49f34db242a1910b7295550a4f86d3d2279d0df/Path-to-file.dll', because it doesn't contain the resource 'opencv2.a'.	

Link to public reproduction project repository

No response

Version with bug

8.0.10 SR3

Is this a regression from previous behavior?

Yes, this used to work in Xamarin.Forms

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

iOS 14.0

Did you find any workaround?

No response

Relevant log output

No response

Copied from original issue dotnet/maui#24686

From @github-actions[bot] on Tue, 10 Sep 2024 09:26:22 GMT

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

From @drasticactions on Tue, 10 Sep 2024 15:41:21 GMT

This is unrelated to the MAUI UI repo, which doesn't control this tooling. That belongs in xamarin-macios.

Does this happen if you do this on a Mac?

@rolfbjarne What do you think?

Did you copy the OpenCV.iOS.Binding.dll from the binding project's bin directory somewhere else? If so, you also need to copy the OpenCV.iOS.Binding.resources directory along with it.

@rolfbjarne Hi, sorry for the delay, I was still looking at the maui repo issue.

If I reference the .dll from it's compilation path, I don't get the warnings about not finding the native libraries. But my maui application still fails to compile showing:

Error (active)		clang++ exited with code 1:
Undefined symbols for architecture arm64:
  "_OBJC_CLASS_$_ALAssetsLibrary", referenced from:
      objc-class-ref in opencv2.a(cap_ios_video_camera.o)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)	AppName (net8.0-ios)	C:\Program Files\dotnet\packs\Microsoft.iOS.Sdk.net8.0_17.5\17.5.8020\targets\Xamarin.Shared.Sdk.targets	1641	

I'm compiling my iOS Binding library with AnyCPU settings. This is what I can see in the compilation path (from where i reference my .dll)

image

Undefined symbols for architecture arm64:
"OBJC_CLASS$_ALAssetsLibrary", referenced from:

This happens because your native library needs to link with the AssetsLibrary framework, so you have to state this in the binding project like this:

<NativeReference Include="opencv2.a">
	<Kind>Static</Kind>
	<ForceLoad>True</ForceLoad>
	<Frameworks>AssetsLibrary</Frameworks>
</NativeReference>

This is what I can see in the compilation path (from where i reference my .dll)

It looks like this is on Windows; binding projects must currently be built on a Mac. If you're building remotely, you need to find the dll + the .resources sidecar on the Mac (it's probably easier to just build on the Mac directly in this case, since it's easier to find the build output).

@rolfbjarne Thank you so much for your help. The flag you suggested made it all work.
By the way, I could build it on Windows + remote mac.

That's great! I'll close this then.