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

后端 未结 1 1445
南方客
南方客 2020-12-19 20:07

I have read the documentation from Xamarin.

And this is my test class in Objective-C:

#import \"XamarinBundleLib.h\"

@implementation XamarinBundleLi         


        
相关标签:
1条回答
  • 2020-12-19 20:35

    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:

    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:

    Hope this helps!

    0 讨论(0)
提交回复
热议问题