tags:

views:

76

answers:

3

Here's the scenario - I have created a custom NSView subclass, and the implementation is in a static library. The class is never referenced from the final executable, only from the Interface Builder XML file. Since it's not referenced, it doesn't get included at link time, and as a result the class can't be found at runtime.

Is there any way to force it to be linked in, other thank link dynamically or compile the class directly into the executable itself?

+1  A: 

You can use the class class method on it, which will mostly be a no-op, but will reference it from your code.

int main(int argc, const char** argv)
{
    [MyClass class]; // There you are! MyClass is now referenced from your code.

    /* ... rest of your main function ... */
}
zneak
That's a good idea. Will that bring in all the functions as well, or do I have to somehow reference the entry points that are used?
Xtapolapocetl
@Matt Spong: It will bring the class and everything the class or its instances use.
zneak
A: 

How about making a pointer to the offending class in the executable? That ought to force it and won't require you to allocate memory etc.

Jon Cage
That could work too. Same question I posed to the answer above - will that bring in the functions as well, or do I need to reference those too?Either way, this looks like it answers my question. I assume that if I need to do it explicitly, taking the address of a function should be enough to coerce the linker into including it. No need to actually call it.
Xtapolapocetl
If you've made a reference to the class, the compiler will need to understand the whole object not just some part of it, so yes, just making a pointer to the class should force the linker to include it all.
Jon Cage
A: 

Trying using -all_load or -force_load flags. See comments in the following post:

http://stackoverflow.com/questions/2906147/what-does-the-all-load-linker-flag-does

Leibowitzn