美文网首页
iOS 杂记(一)

iOS 杂记(一)

作者: 我的昵称是小狼 | 来源:发表于2018-11-16 15:43 被阅读0次

关于编译的时候link参数

代码由源码到可执行文件的过程,简单的说分为四个步骤

  1. 预处理
  2. 编译
  3. 汇编
  4. 链接

这几步过程通常由我们的IDE也就是Xcode帮我们悄悄的完成了,但是有时候我们也会遇到一些问题,需要我们自己指定某些参数来完成.
今天主要说一下Xcode BuildSetting 中的Other Link Flags 参数,这个参数顾名思义是link的时候指定的参数

If you're seeing a "selector not recognized" runtime exception when calling a category method that is implemented in a static library, you are hitting the link-time build issue described here, and need to add the -ObjC linker flag to your project.
A UNIX static library is just a collection of object files. Normally the linker only pulls in an object file from a static library if doing so would resolve some undefined symbol. Not pulling in all object files reduces the size of the final executable.
The dynamic nature of Objective-C complicates things slightly. Because the code that implements a method is not determined until the method is actually called, Objective-C does not define linker symbols for methods. Linker symbols are only defined for classes
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.

静态库本质上就是一系列的目标文件的集合,通常linker会从静态库里加载这些目标文件.
因为OC是动态语言,所以OC没有为每个方法建立符号表,而是为每个类建立链接符号。这样的话静态库中定义了已存在的类的分类,链接器就以为这个类存在了,不会将分类和核心类代码关联(合并)起来.
这个时候就需要我们自己手动指定link参数了

Other Link Flags :

-ObjC:这个链接参数,是针对OC的,这个参数会在link的时候将静态库的所有的OC的类和分类全部链接到可执行文件中
-all_load : 这个链接参数,会让linker从它能扫描到的静态库内加载所有的目标文件
-force_load : 强制link链接某个指定的库,需要指定库的路径.例如 : -force_load $(PROJECT_DIR)/yourframewokname.framework/yourframeworkname

一般我们开发的时候默认使用-ObjC的链接参数,不过当你有某个静态库,这个静态库内包含了某个不包含任何OC的代码文件的时候,这个静态库的这个文件可能不会被链接到可执行文件内.你可以这些个链接参数来更改这个行为.

PS: 不推荐使用-all_load这个链接参数,因为这样会使你的可执行文件体积变大.而且可能会引起符号冲突问题.
推荐使用-force_load的方式.不过我们还有一个更加取巧的方法,你可以在这个文件内声明一个空的OC类,这样-ObjC参数就会去link这个类了.


参考资料 StackOverFlow

相关文章

  • 2018-01-08

    title: ios杂记 date: 2015-12-20 #写作时间 description: 逗指导的IOS...

  • Objc中国 ios 逆向论坛 北海道杂记 大神博客 大神推荐 博客

  • iOS 杂记(一)

    关于编译的时候link参数 代码由源码到可执行文件的过程,简单的说分为四个步骤 预处理 编译 汇编 链接 这几步过...

  • iOS 杂记(一)

    相信肯定很多人面试的时候会被问到 delegate为什么要用 weak? 答案大家都知道,这里就不说了. 但是如果...

  • iOS杂记

    1.内存地址转对象: 通过 Xcode 中的 【Debug View Hierarchy】,根据内存地址(比如:0...

  • ios 杂记

    阅读目录 1.键盘弹出隐藏通知.检测一段代码运行时间的方法。3.UILabel显示html文本4.改变导航栏按钮的...

  • ios 杂记

    http://upload-images.jianshu.io/upload_images/1338042-dd4...

  • IOS 杂记

    ·楼主经历过C 开发,java 开发,现在开始ios开发。准备只学习swift 语言,第一个阶段的目标是完成遥控器...

  • iOS杂记

    有些很简单的就随便看看了,我这也是遇到了就记下来 1.iOS开发加载图片·imageNamed和imageWith...

  • iOS杂记

    http网络请求plist文件配置 plist的sourceCode方式打开用户权限 NSPhotoLi...

网友评论

      本文标题:iOS 杂记(一)

      本文链接:https://www.haomeiwen.com/subject/ercmfqtx.html