Is -lall_load in the host project the right way to get Categories in static libraries to load? I'm sort of disappointed that Xcode can't figure it out from the code. I'd understand if the category methods were only being referenced at run time, but I'm actually referencing them in the code.
It's confusing. -ObjC should do the job for you, even for categories. There was a bug in the linker back a couple of versions of Xcode ago which meant it didn't work and a technote was published advising you use -all-load or -force_load. Since then the bug was fixed, and then it was broken again, and now i think it's fixed again although I can't find the post or release note which I read which said that.
So the difference is, linker symbols in the .o files are only for the classes used in those .o files. The linker with no flags links in just those classes it finds (although it links in every single method of those classes). With categories, you're not changing the classes you use, eg if you put a category on NSString for instance, the .o still just references NSString, it doesn't know that there is code in the library which extends NSString and which you need to load as well. I don't know if it couldn't know that, but it doesn't know that. So -ObjC flag tells the linker to link every objective-C class and category it finds in the library. The bug which was introduced meant that classes were linked but categories were not. The all-load flag says just link in everything from the library, obj-c or not, whether its used or not. That total sledgehammer worked around the bug.
So -ObjC should work, if it doesn't what version of the tools are you on?
I've stopped using categories in static libraries for exactly this reason. Having to use -ObjC links in my entire static helper library I've been building up for years, and the code bloat is awful. So I've stopped entirely now and have class methods on a helper object, it's more typing, not so elegant, but it works and I get the minimal code linked in. Since I work almost universally on iOS I think that's a good thing.
The other one of these which gets me is IB files. You use a class in a static library in an IB file, say MyCuteViewSubclass, but you never reference it in code, so the class isn't linked in and it fails when IB tries to load it. I believe even if you call instance methods on that library class, that's not enough to get it linked, you have to call a class method, like alloc, that's certainly been my observation. Again the usual advice is to use -ObjC, but I have, for each project, a hack file which just calls [ MyCuteViewSubclass class ] for each such class. That code isn't even every executed, but it's linked in, and that's enough. I wish that compiling xibs would produce a .c file, or .o file or .somekindoflinkerhint file which would ensure the classes referenced in the XIB are linked also.
|