iDevGames Forums
NSApplication principal class not seen in static lib - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Programming Languages & Scripting (/forum-8.html)
+--- Thread: NSApplication principal class not seen in static lib (/thread-460.html)



NSApplication principal class not seen in static lib - AnotherJake - Jan 7, 2010 03:57 PM

So I've finally decided I've had enough with dynamically loaded libraries and am moving my remaining code base to a static library. One of the things I do is override sendEvent, which requires sub-classing NSApplication and setting the app's principal class to my subclass. Worked great in my dynamically loaded "framework", but now that it's in a static library, when the application attempts to launch, it says it cannot find the principal class and exits. Bummer. So after some fiddling around, I found that if I just trick it a little, it sees it. The way I'm doing the "trick" is like this:

Code:
#import "tqMacApplication.h" // <-- part of static library

- (void)thisMethodIsNeverCalled
{
    tqMacApplication *tqMacApp = [[tqMacApplication alloc] init];
}

That's all I have to do to make the class visible on launch (the principal class is set to tqMacApplication). As the method implies, I never actually call it. Just having the alloc there is enough to do the trick.

Could someone explain to me why this is so?

Also, is there a more elegant way of getting the class noticed on launch for the principal class? The way I'm doing it right now obviously reeks of hackishness.


NSApplication principal class not seen in static lib - AnotherJake - Jan 7, 2010 05:35 PM

Well, it appears that I may have found out why this is so at this link. Still not ideal because it loads *everything*, but for my situation, it might be just fine. However, I tried adding the -ObjC flag to the other linker flags in the application which uses the library (not the library itself) and it still doesn't work. Hmm...


NSApplication principal class not seen in static lib - DoG - Jan 7, 2010 05:37 PM

You can use -Objc in "other linker flags" to make sure that all ObjC classes are loaded in all linked modules, otherwise it's done lazily, so if there's no module actually referencing that class, the ObjC runtime won't ever get to see it, as the "static" linker (i'm sure OSC will complain about this) will simply skip it.

So, your method call, while never actually used, forces the symbols to be linked, so that a runtime function like NSClassFromString(), which is how the "principal class" is got from the class name, sees it.

EDIT: dammit too late Wink


NSApplication principal class not seen in static lib - AnotherJake - Jan 7, 2010 05:45 PM

Thanks for replying though DoG, I really appreciate it. Smile

I cleaned the project and built it again with -ObjC but it still doesn't find the class unless I add the method call. Here's a snippet of the build result for sanity:

Code:
Ld build/Debug/BasicTest1.app/Contents/MacOS/BasicTest1 normal x86_64 ... -mmacosx-version-min=10.6 -ObjC -framework Cocoa -framework QuartzCore ...

So -ObjC is in there, but doesn't seem to be doing what I expect.

[edit] Whoops! I just missed this below that:
Code:
clang: warning: argument unused during compilation: '-ObjC'

WTF?


NSApplication principal class not seen in static lib - AnotherJake - Jan 7, 2010 05:50 PM

Works with LLVM GCC 4.2 though... Must be a Clang issue that I am unaware of.