Building Objective-C static libraries with categories

来源:互联网 发布:图片文档识别软件 编辑:程序博客网 时间:2024/05/20 21:57
Technical Q&A QA1490

Building Objective-C static libraries with categories

// 用类目 编译Object-C 静态库

Q:  Why do I get a runtime exception of "selector not recognized" when linking against an Objective-C static library that contains categories?

// 为什么我得到一个运行时的异常处理“选择器不被识别”,当我连接一个包含类目的Objective-C 静态库.

A: Why do I get a runtime exception of "selector not recognized" when linking against an Objective-C static library that contains categories?

The "selector not recognized" runtime exception occurs due to an issue between the implementation of standard UNIX static libraries,

// 运行时“选择器不被识别”异常信息出现是由于一个问题在实现标准的静态库

 the linker and the dynamic nature of Objective-C.

// 这种链接和Object-C自认的特征

 Objective-C does not define linker symbols for each function (or method, in Objective-C) - instead, linker symbols are only generated for each class. 

// Objective-c 不为每一个函数(方法,在Object-C)定义连接元素,换句话说,连接元素仅仅在每个类产生。

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. 

// 目标链接静态库,必须传一个-Objc指针 指向连接器

This flag causes the linker to load every object file in the library that defines an Objective-C class or category.

// 这个标记 引起链接器 去加载每个定义一个定义了Object-c类或类目的在静态库中的对象文件。

 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.

// 它将允许成功的创建有效的Objective-C静态库-在已存在类上类目

 Follow these steps to pass -ObjC to the linker:

// 通过这两步去传一个objc到连接器

  1. In Xcode, double-click the target's name under "Targets" in the Project window.//在Xcode双击 在工程窗口 Targets下的target

  2. Choose the Build pane from the ensuing Info window.// 

  3. Scroll down to the Other Linker Flags build setting under the Linking collection and set its value to -Obj

Figure 1  Target Build pane: Other Linker Flags

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.

-all_load forces the linker to load all object files from every archive it sees, even those without Objective-C code. -force_load is available in Xcode 3.2 and later. It allows finer grain control of archive loading. Each -force_load option must be followed by a path to an archive, and every object file in that archive will be loaded.

原创粉丝点击