为什么要用-all_load&-ObjC

简介:

为了减少工作量复用部分代码,于是乎我们开始选择重构整个项目,把可以公用的代码放在一起打包成一个
静态库子项目在其他的项目中使用。介绍这部分内容的文章在网上很多,各位可以G一下细看。

但是每次在加入静态库的时候都会在other linker flag里设置两个值:-all_load和-ObjC。
很奇怪为什么要这样做,而且有的时候什么都不设置也不影响静态库的使用。
所有使用了这个静态库的项目都没有出现任何的问题,即使不设置那两个flag值。

于是拜求G大神,终于找到了结果。
关于-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后面必须跟一个只想静态库的路径。

读了以上你明白了吧。
无论如何还是把那两个flag的值都加上吧。这样可以保证你哪天突发奇想加了一堆category什么的oc牛X语法也不至于项目崩溃。

亲们,觉得有价值就顶起来吧

欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 330987132 | Go:217696290 | Python:336880185 | 做人要厚道,转载请注明出处!http://www.cnblogs.com/sunshine-anycall/archive/2013/05/02/3055097.html
相关文章
|
5月前
|
Unix 编译器 C语言
-ObjC引起的duplicate symbol _OBJC_IVAR
-ObjC引起的duplicate symbol _OBJC_IVAR
40 1
Undefined symbols for architecture x86_64: "_OBJC_CLASS_$_ZMCertification", referenced from:解决方法
Undefined symbols for architecture x86_64: "_OBJC_CLASS_$_ZMCertification", referenced from:解决方法
194 0
|
PyTorch 算法框架/工具
torch中报错:AttributeError: 'builtin_function_or_method' object has no attribute 'detach'怎么解决?
这个错误信息 "AttributeError: 'builtin_function_or_method' object has no attribute 'detach'" 表示你尝试在一个内置函数或方法对象上调用 detach() 方法,而这种对象没有这个属性。 detach() 是 PyTorch 张量和变量的方法,允许它们从计算图中分离出来,因此不能在其他类型的对象上调用。要解决这个错误,请确保你正在一个 PyTorch 张量或变量上调用 detach() 方法。
1028 0
relocation R_X86_64_PC32 against symbol lua_newstate can not be used when making a shared object
relocation R_X86_64_PC32 against symbol lua_newstate can not be used when making a shared object
248 0
undefined reference to `swr_init+
undefined reference to `swr_init+
107 0
Load和Initialize的区别和使用
Load和Initialize的区别和使用
173 0
loaded the "xxx" nib but the view outlet was not set 错误的解决办法。
loaded the "xxx" nib but the view outlet was not set 错误的解决办法。
214 0