`

使用类别扩展方法编译静态库.a后不可用的问题

 
阅读更多

http://developer.apple.com/library/mac/#qa/qa1490/_index.html

 

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. 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. Follow these steps to pass -ObjC to the linker:

  1. In Xcode, double-click the target's name under "Targets" in the Project window.

  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 -ObjC.

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.



Document Revision History


DateNotes
2010-04-30

Describes how to pass the "-ObjC" option to the linker. Updated for Mac OS X v10.6 and later.

2006-10-03

Changed "and" to "an" in order to make the content correct.

2006-09-25

New document that describes how to properly build Objective-C static libraries that contain categories on existing classes.


分享到:
评论

相关推荐

    开发PHP扩展详细教程

    第六、七行将ICBC的静态库加入到编译环境中 3.在php_icbc.h中声明我们要导出的函数icbc_sign、icbc_vsign、icbc_getCertID #ifndef PHP_ICBC_H #define PHP_ICBC_H extern zend_module_entry icbc_module_...

    易语言程序免安装版下载

    “在库中的对应命令名”请务必准确填写静态库中公开导出的符号名称(C函数(cdecl)编译后,符号名称通常是在函数名称前加下划线(_));“在库中的对应命令名”以@开头表示以cdecl方式调用,否则表示以默认的stdcall...

    Delphi5开发人员指南

    4.5 定义公共体系结构:使用对象库 93 4.5.1 考虑应用程序的体系结构 93 4.5.2 Delphi固有的体系结构 94 4.5.3 体系结构的例子 94 4.5.4 子窗体TChildForm 94 4.5.5 数据库基础模式窗体TDBMode- Form 96 4.5.6 ...

    php网络开发完全手册

    2.7.2 使用include与include_once语句 2.7.2 进行文件包含 44 2.8 小结 45 第3章 PHP的数据类型与操作 46 3.1 数据类型的介绍 46 3.1.1 标量数据类型 46 3.1.2 合成数据类型 49 3.1.3 特殊的数据类型 50 3.1.4 数据...

    谭浩强C语言程序设计,C++程序设计,严蔚敏数据结构,高一凡数据结构算法分析与实现.rar

    3.5.4 字符数据在内存中的存储形式及使用方法 41 3.5.5 字符串常量 41 3.5.6 符号常量 42 3.6 变量赋初值 42 3.7 各类数值型数据之间的混合运算 43 3.8 算术运算符和算术表达式 44 3.8.1 C运算符简介 44 3.8.2 算术...

Global site tag (gtag.js) - Google Analytics