您的位置:首页 > 其它

-all_load -force_load -ObjC

2014-08-15 13:15 113 查看
[http://blog.csdn.net/leonpengweicn/article/details/6799994]

生成静态库的一些问题-all_load

1、使用类目

在我们的静态库中涉及到 类目 catagory的使用时,会崩溃;

此时我们需要设置project的Info里面的Link Flag处,增加-all_load,这样会链接所以存在的symbol;

这是我们常用的一种处理方法,除此之外我们还可以使用以下方法:

若我们使用了类目

"NSObject+SBJSON.h"

我们在h,m文件分别增加以下声明

@interface DummyClass_NSObject_SBJSON {}

@end

@implementation DummyClass_NSObject_SBJSON

@end

2、使用nib

若封装静态库的时候我们使用了xib文件,亦有可能会出现此种形式的崩溃

Unknown class XXX in Interface Builder file

此处由于在代码中class XXX你并未引用过,具体的原理我也没有特别弄清楚,还希望高手帮助我们解释一下;

我暂时做的处理时在接口处优先将这些 class 执行一个方法, 比如 [Class class];

这个问题的原因应该是由于原先我的代码中并没有调用到 class XXX 相关方法;

而静态库的一个优点是:链接器可以从静态库中只取出需要的部分来做链接。故没有链接 这些没有调用过方法的 class;

暂时用上面的解决方案解决了,不知道有没有更好的方法来解决这个问题;

以下两种情况都可以设置project的Info里面的Link Flag处,增加-all_load,解决

[http://www.2cto.com/kf/201311/260941.html]

-all_load -force_load -ObjC

-ObjC:

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.

这个flag告诉链接器把库中定义的Objective-C类和Category都加载进来。这样编译之后的app会变大(因为加载了其他的objc代码进来)。
但是如果静态库中有类和category的话只有加入这个flag才行。

-all_load

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.

这个flag是专门处理-ObjC的一个bug的。用了-ObjC以后,如果类库中只有category没有类的时候这些category还是加载不进来。变通方法就是加入-all_load或者-force-load。-all_laod会强制
链接器把目标文件都加载进来,即使没有objc代码。-force_load在xcode3.2后可用。但是-force_load后面必须跟一个只想静态库的路径。

-force_load
When someone uses "-all_load", every imported library is forced into memory whether its needed or not. A much better technique is "-force_load":

-force_load $(BUILT_PRODUCTS_DIR)/libarc.a

This insures that ONLY your library is loaded, not every library.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: