Binding the Swift library ATInternet to an iOS project compilation error: Native linking failed

Hi,

I am trying to bind the new version of the ATInternet's ATTracker (which is now made in Swift) to a Xamarin.Forms project.

I followed a tutorial about binding a Swift library to Xamarin.iOS project and replaced the native reference by the ATInternet library. I then generated the ApiDefinition.cs file with Sharpie on Mac (and simplified it to a minimal version so it can only initialize the default tracker). I get the following ApiDefinition.cs file:

//// @interface ATInternet : NSObject
[BaseType(typeof(NSObject))]
//[DisableDefaultCtor]
public interface ATInternet
{
    // @property (readonly, nonatomic, strong) Tracker * _Nonnull defaultTracker;
    [Export("defaultTracker", ArgumentSemantic.Strong)]
    Tracker DefaultTracker { get; }

    // @property (readonly, nonatomic, strong, class) ATInternet * _Nonnull sharedInstance;
    [Static]
    [Export("sharedInstance")]
    ATInternet SharedInstance();

    // -(Tracker * _Nonnull)tracker:(NSString * _Nonnull)name __attribute__((warn_unused_result));
    [Export("tracker:")]
    Tracker Tracker(string name);
}

// @interface Tracker : NSObject
[BaseType(typeof(NSObject))]
public interface Tracker
{
    // -(void)setConfig:(NSDictionary<NSString *,NSString *> * _Nonnull)configuration override:(BOOL)override sync:(BOOL)sync completionHandler:(void (^ _Nullable)(BOOL))completionHandler;
    [Export("setConfig:override:sync:completionHandler:")]
    void SetConfig(NSDictionary<NSString, NSString> configuration, bool @override, bool sync, [NullAllowed] Action<bool> completionHandler);
}

Then in my Xamarin.iOS project is use it as this:

public partial class ViewController : UIViewController
{
    protected ViewController(IntPtr handle) : base(handle) {}

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        var _instance = XamarinBindingLibrary.ATInternet.SharedInstance();
        //var _tracker = _instance.DefaultTracker;
    }
}

But everytime i try to compile the project i get the following error:

2>  /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++  -framework Foundation -framework QuartzCore -framework CloudKit -framework UIKit -framework CoreGraphics -F /Users/sfdigital/Library/Caches/Xamarin/mtbs/builds/SampleUsing/1978f5377351d84fde88f310ab60b786/obj/iPhoneSimulator/Debug/mtouch-cache -framework Tracker -F /Users/sfdigital/Library/Caches/Xamarin/mtbs/builds/SampleUsing/1978f5377351d84fde88f310ab60b786/obj/iPhoneSimulator/Debug/mtouch-cache -framework TrackerExtension -weak_framework Intents -weak_framework CFNetwork -Xlinker -rpath -Xlinker @executable_path/Frameworks /Users/sfdigital/Library/Caches/Xamarin/mtbs/builds/SampleUsing/1978f5377351d84fde88f310ab60b786/obj/iPhoneSimulator/Debug/mtouch-cache/x86_64/main.o /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.4/SDKs/MonoTouch.iphonesimulator.sdk/usr/lib/libmonosgen-2.0.a /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.4/SDKs/MonoTouch.iphonesimulator.sdk/usr/lib/libxamarin-debug.a -force_load /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.4/SDKs/MonoTouch.iphonesimulator.sdk/usr/lib/libapp.a -Wl,-pie -arch x86_64 -gdwarf-2 -std=c99 -I/Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.4/SDKs/MonoTouch.iphonesimulator.sdk/usr/include -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator11.2.sdk -Qunused-arguments -fobjc-legacy-dispatch -fobjc-abi-version=2 -mios-simulator-version-min=9.3 -lz -liconv -o /Users/sfdigital/Library/Caches/Xamarin/mtbs/builds/SampleUsing/1978f5377351d84fde88f310ab60b786/bin/iPhoneSimulator/Debug/SampleUsing.app/SampleUsing -dead_strip -u _xamarin_timezone_get_data -u _xamarin_log -u _UIApplicationMain -u _xamarin_get_block_descriptor -u '_OBJC_CLASS_$_ATInternet' -u '_OBJC_CLASS_$_Tracker' -u _mono_pmip -u _xamarin_dyn_objc_msgSend -u _xamarin_dyn_objc_msgSendSuper -u _xamarin_dyn_objc_msgSend_stret -u _xamarin_dyn_objc_msgSendSuper_stret
2>  Undefined symbols for architecture x86_64:
2>    "_OBJC_CLASS_$_Tracker", referenced from:
2>       -u command line option
2>    "_OBJC_CLASS_$_ATInternet", referenced from:
2>       -u command line option
2>  ld: symbol(s) not found for architecture x86_64
2>  clang: error: linker command failed with exit code 1 (use -v to see invocation)
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\Xamarin\iOS\Xamarin.iOS.Common.targets(777,3): error : Native linking failed, undefined Objective-C class: Tracker. The symbol '_OBJC_CLASS_$_Tracker' could not be found in any of the libraries or frameworks linked with your application.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\Xamarin\iOS\Xamarin.iOS.Common.targets(777,3): error : Native linking failed, undefined Objective-C class: ATInternet. The symbol '_OBJC_CLASS_$_ATInternet' could not be found in any of the libraries or frameworks linked with your application.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\Xamarin\iOS\Xamarin.iOS.Common.targets(777,3): error : Native linking failed. Please review the build log.

Any idea of how i can get rid of this error?
Does i have to make a new Swift Library that bind their library but with the requirement stated in the tutorial?

Tagged:

Posts

  • dgarciasaraidgarciasarai ESMember

    Hey!

    If you're creating a binding library and you are importing your .framework file, maybe you should have simulator and device architectures inside (fat library).

    Regards

  • mharpermharper Member ✭✭

    Take a look in your ATTracker-Swift.h file (in the framework's Headers subdirectory). Find the entries for Tracker and ATInternet. Is there a line before each that looks like (not exactly but similar):

    SWIFT_CLASS("_TtC15ATInternet11Tracker")
    

    If so, then you may need to add the Name attribute with the SWIFT_CLASS name to those two class definitions in your ApiDefinition.cs file:

    [BaseType(typeof(NSObject), Name = "_TtC15ATInternet11Tracker")]
    public interface Tracker
    
Sign In or Register to comment.