在静态库中链接objective-c类别 [英] linking objective-c categories in a static library

查看:143
本文介绍了在静态库中链接objective-c类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为iOS应用程序开发一个插件。我正在将它编译成.a文件,然后由主xcode项目使用。



到目前为止,我已在此库中创建了一个UIDevice类的类别。当我使用这个库运行主项目时,由于无法识别的选择器而崩溃



- [UIDevice platform]:无法识别的选择器发送到实例



平台是我通过类别添加的功能之一。



所以我认为它根本没有链接这些功能并添加了交流功能与UIDevice类别相同的文件然后从我的代码中调用它。



这次主项目运行正常...所以我想也许这是我做的其他事情并删除了C函数。但是,由于无法识别的选择器,它再次崩溃..



我的问题:
为什么xcode忽略类别定义,除非我调用在相同的文件?



是否有一个xcode设置我可以更改,使其包含来自UIDevice类别的这些方法,无论我是否从该文件调用函数? / p>

欢呼

解决方案

查看构建带有类别的Objective-C静态库


Objective-C没有为每个函数定义链接器符号(或在Objective-C中为
方法) - 相反,每个链接器符号只生成
类。如果使用类别
扩展预先存在的类,则链接器不知道将核心
类实现的对象代码与类别实现相关联。这可以防止在生成的应用程序中创建的
对象响应类别中定义的
选择器。



解决此问题,链接到静态库
的目标必须将-ObjC选项传递给链接器。
此标志使链接器
加载库中定义Objective-C $的每个目标文件b $ b类或类别。虽然此选项通常会产生更大的
可执行文件(由于额外的对象代码加载到
应用程序中),但它将允许成功创建包含类别的有效
Objective-C静态库现有的
类。








重要:对于64位和iPhone OS应用程序,有一个
链接器错误,它阻止-ObjC从仅包含类别而不包含类的静态
库中加载对象文件。解决方法
是使用-all_load或-force_load标志。


来源:@albertamg(在静态库中链接objective-c类别


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

解决方案

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)

这篇关于在静态库中链接objective-c类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆