[Interest] Objective C Category for QCocoaApplicationDelegate undefined symbols

Till Oliver Knoll till.oliver.knoll at gmail.com
Mon Jul 6 19:38:21 CEST 2015


> Am 01.07.2015 um 10:54 schrieb Croitor Alexandru <placinta at gmail.com>:
> 
> Hi.
> 
> I'm trying to add an Objective C category to the QCocoaApplicationDelegate class that is defined by the Mac OS X Cocoa platform plugin, to add / override some methods. 

I am not an Obj-C expert, but I'd say you need to link at Build time with the class you're trying to add a Category to. You could try to extend the (non-existent) class "MickeyMouse" with a Category and you should see similar linker errors.

But basically your own findings already confirm the above.

That said, I am not sure if Obj-C Categories actually work with code that is dynamically linked by the application itself (aka "plugin loading", dlopen() etc.). I understand that with Categories you can "add" methods even to class instances (such as instances of, say, NSString - and all subclasses thereof) that are instanciated by other libraries that don't even know about your Category. In other words, all happens at runtime. But maybe the "system linker" triggers some magic stuff, that is, "injecting" an extended message handler which first deals with your Category defined messages (aka "methods", for the Obj-
C foreigners here on the list ;)) before passing them on to the original implementation message handler? And maybe that only works with libraries that you linked (dynamically) at Build time, but not at Runtime (with dlopen)? Not sure...

But let's assume that adding a Category to plugins works and all the linker needs is a "class definition & implementation", also in the actual application ("plugin host").

You already tried to link with QCocoaApplicationDelegate, which didn't work.

But doesn't that delegate class derive from some NSApplicationWhateverDelegate? Which is probably a "Protocol" anyway, so bad luck - or can one add Categories to Protocols?

Which led me to

  http://stackoverflow.com/questions/1521267/defining-categories-for-protocols-in-objective-c

and one answer talks about "ExtObjC" (an additional library, I guess). While in general you cannot extend Protocols with Categories (in vanilla ObjC), that library might help you...

Anyway, my general idea, as you have figured out, is to extend QCocoaApplicationDelegate's "base class" (or Protocol), which I guess is an NSWhatever class/protocol. Then simply link your application explicitly with -lcocoa (or the correct framework/if needed).

Maybe that gets you some ideas...

Cheers,
  Oliver


More information about the Interest mailing list