天天看點

為什麼要用-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: 572064792 | Nodejs:329118122 做人要厚道,轉載請注明出處!

本文轉自張昺華-sky部落格園部落格,原文連結:http://www.cnblogs.com/sunshine-anycall/archive/2013/05/02/3055097.html,如需轉載請自行聯系原作者

繼續閱讀