How to Bind an Objective-C Static Library to Xamarin.iOS

How to bind an Objective-C static library to Xamarin.iOS?

I wrote a very detailed blog post about creating a static library from ObjC code last year that works on Xamarin.iOS binding projects and you can find it here (just in case :wink::wink:).

That being said if you already have a fat static library in your hands and it is already added into your Xamarin.iOS Binding Project as shown here:

binding image

The issue could be that your libxyz.linkwith.cs is missing some information, if it looks like this:

using ObjCRuntime;
[assembly: LinkWith ("libFoo.a", SmartLink = true, ForceLoad = true)]

it is definitely missing some important information about the architectures supported by your fat library (it is missing the second argument target), you can use the following command to retrieve what architectures your current static library supports

xcrun -sdk iphoneos lipo -info path/to/your/libFoo.a

and you should get something like this as output

Architectures in the fat file: Foo/libFoo.a are: i386 armv7 x86_64 arm64

So we know this static library supports i386 armv7 x86_64 arm64 and we should provide our LinkWith attribute the supported archs by providing the second argument target as follows:

using ObjCRuntime;
[assembly: LinkWith ("libFoo.a", LinkTarget.ArmV7 | LinkTarget.Arm64 | LinkTarget.Simulator | LinkTarget.Simulator64, SmartLink = true, ForceLoad = true)]

Also make sure that the first parameter of the LinkWith attribute matches your static library file name ("libFoo.a" in my case).


The other thing I would suggest double checking is that the Build Action of your static library (libFoo.a in my case) is correctly set to ObjcBindingNativeLibrary as show here:

binding image

Hope this helps!

Xamarin.iOS Binding an iOS Objective-C Library resulted in empty namespace

Native Binding Project Compiles Library With No Methods Or Classes

Fixed the problem. The issue was that the Binding Project was in the same solution as the Xamarin.iOS project so it was not referencing the project correctly. Removed the binding project from the solution, then added the binding project DLL as a reference and now it properly sees the namespace and the methods. I will fix the other issues you mentioned like required frameworks. Thanks for your help! This issue can be closed.

This page helped:

https://forums.xamarin.com/discussion/81795/ios-obj-c-binding-cant-see-namespace

Binding a static library into a Xamarin iOS project

I thought I'd answer my own question here - after getting some help from Xamarin support, it looks like I might have mixed up how I created the static library - I had to make sure the files were compiled as 'C' files, instead of C++. After I generated the static library correctly, the compiler was able to recognize the missing symbols.

Binding iOS Static Library to Xamarin.iOS and using the dll

Finally figured out the error!!

    [assembly: LinkWith (..., IsCxx = true, Frameworks = "CoreAudio AudioToolbox", LinkerFlags = "-lstdc++ -lz" )]

In iOS Binding Projet

Options > Build > Compiler > Addition Arguments

    -cxx -gcc_flags "-stdlib=libstdc++ -L${ProjectDir} -lMylibrary -force_load ${ProjectDir}/libMylibrary.a”

In my demo application

Options > iOS Buidl > Addtional mtouch Arguments

    --gcc_flags="-stdlib=libstdc++"

By giving the proper flags in all the places it resolved my issue.



Related Topics



Leave a reply



Submit