views:

35

answers:

3

I'm doing an iPhone plugin project where I build a static library, let's call it lib1.a, which I provide to other programmers.

When they link lib1.a into their project, they may also link lib2.a, which they build themselves based on a header file I give them. This header only contains a "hook" function which instantiates an obj-c object.

This all works fine, but I'd like for the project linking lib1.a not to have to link lib2.a. Keep in mind that iOS only supports static libraries, and I don't want to provide several versions of lib1.a.

A: 

If you made the hook register itself, then the presence of the hooking code would self register, the lack thereof, would not. Obviously, only call registered hook functions from lib1.a and you're good to go:-

You don't need to do anything complicated, just use the basic features of c/c++/obj-c to get this kind of behaviour.

Chris Becke
Thanks for your reply. With this approach, how would the code in lib2.a that registers the hook be executed?
Jonny Boy
statically allocated c++ class is one way.
Chris Becke
Thanks, Chris. That would work. However it's getting kind of messy, as lib2 should ideally not know or care about lib1. Also, lib2 will be obj-c-oriented internally, so it doesn't feel too good to ask users of lib1 to add C++ code to their lib.
Jonny Boy
+1  A: 

Can't you, in objective-C, use a static library to add a Category - with new methods, to an existing objective-C class.

So, if lib1.a contains

@interface SomeObjectThatWantsToCallback

Then, lib2.a would contain the definition of

@interface SomeObjectThatWantstoCallBack (CallbackImpl)
-(void)HookProc:{
}

Now, if code in SomeObjectThatWantsToCallBack in lib1.a needs to call the HookProc it can do

if( [self respondsToSelector: @selector( HookProc: )])
  [self HookProc];

Well, something like that. Assuming the lib2.a code has been added the category should have extended the class with the method.

Chris Becke