陈斌彬的技术博客

Stay foolish,stay hungry

iOS 编译的一些细节

一、使用 arc 必须用 LLVM3.0,LLVM3.0 多了一个给力的新东西,叫Automatic Reference Counting。开启这个选项之后,LLVM 会在编译阶段自动帮开发者完成“手动”的内存管理。同时,所有代码中的 retain,release 和 autorelease 会被标记成错误,也就是说,我们完全不需要用以前头蛋俱疼的方式管理内存了。

 ARC 不仅可以修复错误,还能增强性能。根据 WWDC 2011 大会的介绍,retain/release 快了2.5倍,@autoreleasepool 快了6倍,objc_msgSend 快了33%。不需要 arc 的文件,加入 fno-objc-arc 编译选项即可。

二、不用 arc 时,编译器用 gccllvm3.0 都可

三、引入了 c++ 静态库,相关使用静态库的 .m 文件都要改为 .mm 文件,包括 appdelegate.mmain.m

四、不要忘记配置 header search paths

五、如果引入的静态库中有 category,在 Other linker flag 中加入 -ObjC-all_load

-all_load Loads all members of static archive libraries.

-ObjC Loads all members of static archive libraries that implement an Objective-C class or category.

-force_load(path_to_archive) Loads all members of the specified static archive library. Note: -all_load forces all members of all archives to be loaded. This option allows you to target a specific archive.

翻译过来就是

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

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

-force_load(包的路径)就是会加载指定路径的静态库文件中的所有成员。所以对于使用 runtime时候的反射调用的方法应该使用这三个中的一个进行 link,以保证所有的类都可以加载到内存中供程序动态调用