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

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

问题描述

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

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

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

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

所以我认为它根本没有链接这些函数,并将一个 c 函数添加到与 UIDevice 类别相同的文件中,然后从我的代码中调用它.

这次主项目运行良好......所以我想也许是我做了其他事情并删除了 C 函数.但是你瞧,它又因为无法识别的选择器而崩溃了..

我的问题:为什么 xcode 会忽略类别定义,除非我调用在同一个文件中声明的函数?

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

干杯

解决方案

查看 用分类构建 Objective-C 静态库:

<块引用>

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

为了解决这个问题,目标链接到静态库必须将 -ObjC 选项传递给链接器. 此标志会导致链接器加载定义Objective-C的库中的每个目标文件类或类别.虽然此选项通常会导致更大的可执行文件(由于加载到应用程序),它将允许成功创建有效的包含现有类别的 Objective-C 静态库类.

<小时><块引用>

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

来源:@albertamg(在静态库)

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天全站免登陆