linking objective-c categories in a static library

Check out Building Objective-C static libraries with categories:

Objective-C does not define linker symbols for each function (or method, in Objective-C) - instead, linker symbols are only generated for each class. If you extend a pre-existing class with categories, the linker does not know to associate the object code of the core class implementation and the category implementation. This prevents objects created in the resulting application from responding to a selector that is defined in the category.

To resolve this issue, the target linking against the static library must pass the -ObjC option to the linker. This flag causes the linker to load every object file in the library that defines an Objective-C class or category. While this option will typically result in a larger executable (due to additional object code loaded into the application), it will allow the successful creation of effective Objective-C static libraries that contain categories on existing classes.


Important: For 64-bit and iPhone OS applications, there is a linker bug that prevents -ObjC from loading objects files from static libraries that contain only categories and no classes. The workaround is to use the -all_load or -force_load flags.

Source: @albertamg (linking objective-c categories in a static library)


I have had the same problem. A method defined in a category defined in a subproject resulted in an unrecognized selector exception. (In fact, this manifested as inability to specify an UILabel subclass in Interface Builder; the XIB contained the class shown in IB (UILabel or UIView, depending on what I dragged there), rather than the class that I have typed in, and that looked as a weird XCode bug.)

The solution that worked for me has been to use -force_load:

In the left-hand panel, select your main project (the root item). On the right, you will see PROJECT and TARGETS. Select TARGETS. Go to "Build Settings"(in the upper bar) -- "Linking" -- "Other linker flags" and, assuming your subproject is called XXXXX, add -force_load ${BUILT_PRODUCTS_DIR}/libXXXXX.a there (the item has two subitems, Debug and Release, but you click on this compound item so that if affects both Debug and Release).

Note that -force_load works for a single library, and you may need to specify a separate -force_load for each subproject library.