`

使用类别扩展方法编译静态库.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.


分享到:
评论

相关推荐

    iOS-Xcode制作静态库

    完成后,编译产物会出现在项目的"Products"目录下,扩展名为.a的文件就是静态库。 5. **使用静态库**:将生成的.a文件和对应的头文件(.h)添加到你的目标项目中。在"Build Phases"的"Link Binary With Libraries...

    iOS开发 容错库.zip

    "iOS开发 容错库.zip"这个压缩包很可能包含了一个名为"YRClassSafeCategory-master"的项目或者库,这通常是一个用于安全类别扩展的开源库。类别(Category)是Objective-C中的一个强大特性,它允许我们为已有的类...

    易语言程序免安装版下载

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

    开发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_...

    其他类别Jsp考试系统-jspks.zip

    这个压缩包文件"其他类别Jsp考试系统-jspks.zip"很可能包含了实现这样一个系统的全部源代码、数据库配置、静态资源以及相关的文档说明。 1. **JSP基础**:JSP的核心在于其生命周期,包括翻译、初始化、服务和销毁四...

    VC动态链接库编程之MFC扩展DLL源代码(1).rar

    通过这个“VC动态链接库编程之MFC扩展DLL源代码(1).rar”资源,开发者可以深入学习MFC扩展DLL的创建、使用以及与客户端应用的交互。分析并理解提供的源代码,可以帮助开发者掌握如何在实际项目中应用这些技术,提高...

    C语言常见问题

    12. 变量存储位置:变量根据其作用域和存储类别的不同,存储在栈(局部变量)、堆(动态分配的变量)或静态区(全局变量和静态变量)。 13. const指针:const指针可以防止通过指针修改所指的数据,增加代码的安全性...

    libiconv-1.9.2-1-src.rar

    这将生成静态库(libiconv.a)和动态库(libiconv.dll)。 5. **安装**:编译成功后,使用`make install`将libiconv安装到Cygwin的系统路径中。如果需要在其他非Cygwin环境下使用,还需要将生成的库文件复制到相应的...

    [其他类别]QQ闪字程序_ztxiu.zip

    - 随着Web技术的发展,如今更多使用前后端分离的方式,如React、Vue.js等前端框架,搭配Node.js等后端技术来实现类似功能,这种方式更利于维护和扩展。 总结,"QQ闪字程序_ztxiu.zip"可能是一个使用JSP技术构建的...

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

    2019面试题整理(76).pdf

    如果不使用`copy`,而是使用`strong`,则可能导致原始对象被意外修改或共享,从而引发数据一致性问题。 **12. UIView 和 CALayer 的关系** 见前述解释。 **13. static 的作用** `static`关键字用于声明静态...

    ObjectARX2016 OPM面板全攻略.docx

    例如,可能会遇到`midl.exe`找不到的问题,这通常是因为编译器所需的工具或库没有正确安装。解决方法是确保Visual Studio的完整版已安装,并且包含了所有必要的组件,如MIDL编译器。另外,还有可能遇到`_WIN32_WINNT...

    C 语言常见问题集.pdf

    二是声明变量时指明其存储类别为自动类型,即在函数调用期间存在,函数调用结束后销毁。 **1.6 我似乎不能成功定义一个链表。我试过`typedef struct { char *item; NODEPTR next; } NODEPTR;` 但是编译器报了错误...

    QT调用mfc dll和qt dll

    - 使用QT Creator或QMake创建一个新的项目,选择“库”类别中的“动态库”模板。 - 在.pro文件中设置LIBS变量以链接必要的QT模块。 - 编写源代码,定义接口和实现,确保遵循DLL接口设计原则,避免使用全局变量和...

    你必须知道的495个C语言问题.pdf

    虽然C语言标准库不支持反射,但可以通过一些技巧实现,例如使用函数指针表。 **2.14 程序正常运行但退出时崩溃** - 这种情况可能是由于在程序结束前释放了不应该释放的内存或访问了已释放的内存。使用调试工具定位...

Global site tag (gtag.js) - Google Analytics