【ios】为什么要在Other Linker Flags添加Flag (eg:-ObjC、-lc++等)?

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dangbai01_/article/details/84100008

一、为什么要在Other Linker Flags添加flag

项目开发中,都会使用一些第三方的静态库,在导入这些第三方类库的时候,其开发文档都会有注明在Build Settings----->Linking------>Other Liker Fliags中添加-ObjC或-all_load或-force_load等。

如果不这样做,运行就会报错从而导致闪退,报错是因为

selector not recognized。

在苹果官方文档有说明

The "selector not recognized" runtime exception occurs due to an issue between the implementation of standard UNIX static libraries, the linker and the dynamic nature of Objective-C. 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.

大致意思是,ios只为【已有的类】生成链接器符号以便于UNIX静态库链接器和objective - c的动态特性之间的关联,如果你在现有类的基础上进行多态扩展了,为了链接器能识别,需要添加对应的flag给扩展分类

二、一些常用的flag的介绍

(1)-ObjC、-all_load、-force_load

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.

大致意思用来加载符合OC标准的三方中的文件

但是有一个问题是,-ObjC有时候有个bug是只从文件中提取扩展而不加载其他普通文件,这时候需要使用-all_load会强制把目标文件都加载进来,

但项目不是使用了一个静态库,这个时候也会出现错误,解决方法一般都是再添加-force_load,需要注意的是-force_load必须指定具体的文件路径,也就是说只完全加载了一个库文件,不影响其余库文件的加载。

(2)-lc++

有种说法是添加-lc++和libc++.tbd的作用是等价的

我就在使用微信mmkv的时候试了一下,发现确实作用一样

然后我们再看libc++

它提供了一个一致且高效的c++标准库,一致意味着兼容性更好,高效意味着速度快,是LLVM项目重新编写,为了替代libstdc++的

那么-lc++的意思是明显了,让我们以libc++的标准去为添加进来的c++文件进行定性分类

猜你喜欢

转载自blog.csdn.net/dangbai01_/article/details/84100008