zoukankan      html  css  js  c++  java
  • iOS:iOS为什么要用-all_load、-ObjC、-force_load

    为了减少工作量复用部分代码,于是乎我们开始选择重构整个项目,把可以公用的代码放在一起打包成一个静态库导入到其他的项目中使用。

    介绍这部分内容的文章在网上很多,各位可以Baidu一下细看。

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


    关于-ObjC的:

    Reference: 
    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的

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

    翻译如下:

    引用:

    不过在64位的Mac系统或者iOS系统下,链接器有一个bug,会导致只包含有类别的静态库无法使用-ObjC标志来加载文件。变通方法是使用-all_load 或者-force_load标志,它们的作用都是加载静态库中所有文件,不过all_load作用于所有的库,而-force_load后面必须要指定具体的文件。

     

    解释:

    这个flag是专门处理-ObjC的一个bug的。用了-ObjC以后,如果类库中只有category分类但是没有类的时候,这些category分类还是加载不进来。

    变通方法就是加入-all_load或者-force-load。

     

    -all_laod:会强制链接器把目标文件都加载进来,即使没有objc代码。

    -force_load:在xcode3.2后可用,而且-force_load后面必须跟一个要加载的静态库的具体路径。

     

    最后总结一下:

    -all_load就是会加载静态库文件中的所有成员,

    -ObjC就是会加载静态库文件中实现一个类或者分类的所有成员,

    -force_load(包的路径)就是会加载指定路径的静态库文件中的所有成员。

    所以对于使用runtime时候的反射调用的方法应该使用这三个中的一个进行link,以保证所有的类都可以加载到内存中供程序动态调用。

  • 相关阅读:
    哈希冲突详解(拉链法,开放地址法)
    哈希冲突详解(拉链法,开放地址法)
    排序算法
    排序算法
    加分二叉树
    加分二叉树
    动态规划
    动态规划
    动态规划
    动态规划
  • 原文地址:https://www.cnblogs.com/XYQ-208910/p/5163407.html
Copyright © 2011-2022 走看看