Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to weak import a class from one static library into another library (Objective-C)

I have a static library, let's call it libSuperCool.a. Inside it I wish to use Millennial Media's SDK, libMMSDK.a. Now for personal reasons, I want my library not to suck up any of that SDK's code, and I want any app using libSuperCool to be able to compile with or without linking against libMMSDK.a; therefore, libSuperCool does not link against libMMSDK.

By default, any references inside libSuperCool to libMMSDK will be strong, requiring that libMMSDK be linked otherwise an executable cannot successfully be linked. For example, one of my source files creates variables of type MMRequest *.

nm -m libSuperCool.a |grep MMReq
    (undefined) external _OBJC_CLASS_$_MMRequest

I found a very interesting post online which explains how to turn an ObjC class reference to a weak one: by using asm(".weak_reference _OBJC_CLASS_$_MMRequest"); inside an .m file, such references will become weak.

nm -m libSuperCool.a |grep MMReq
    (undefined) weak external _OBJC_CLASS_$_MMRequest

However after doing this, I tried to build a simple app that links libSuperCool. I still get the undefined symbol error on _OBJC_CLASS_$_MMRequest.

Are the symbols required to be resolved at link time and if so, why? I can quite easily declare my variables as id and get my end result, this is Objective-C after all.

like image 703
androidguy Avatar asked Nov 01 '22 06:11

androidguy


1 Answers

From glandium.org:

Add -undefined dynamic_lookup to Other linker flags.

like image 94
Nick Avatar answered Nov 15 '22 06:11

Nick