Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linking objective-c categories in a static library

I am developing a plugin for an iOS application. I am compiling it into a .a file which is then used by the main xcode project.

So far I have create a category of the UIDevice class in this library. When I run the main project using this library it crashes due to an unrecognized selector

-[UIDevice platform]: unrecognized selector sent to instance

platform is one of the fuinctions I added via the category.

So I thought it wasn't linking those functions at all and added a c function to the same file as the UIDevice category then called it from my code .

This time the main project ran fine... So I thought maybe it was something else i did and removed the C function. But lo and behold it crashed again due to unrecognized selector..

My questions: Why does xcode ignore the category definition unless I call a function declared in the same file?

Is there an xcode setting i can change to make it include these methods from the UIDevice category regardless of whether I call a function from that file or not?

cheers

like image 924
micken Avatar asked Jul 25 '11 18:07

micken


People also ask

How static library is linked?

Static libraries are either merged with other static libraries and object files during building/linking to form a single executable or loaded at run-time into the address space of their corresponding executable at a static memory offset determined at compile-time/link-time.

What does ObjC linker flag do?

The -ObjC Linker Flag Passing the -ObjC option to the linker causes it to load all members of static libraries that implement any Objective-C class or category. This will pickup any category method implementations. But it can make the resulting executable larger, and may pickup unnecessary objects.

What is the format of a static library in C?

A static library, e.g. libfoo. a is not an executable of any kind. It is simply an indexed archive in unix ar format of other files which happen to be ELF object files.


1 Answers

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)

like image 97
albertamg Avatar answered Sep 19 '22 23:09

albertamg