1. 程式人生 > >【ios】為什麼要在Other Linker Flags新增Flag (eg:-ObjC、-lc++等)?

【ios】為什麼要在Other Linker Flags新增Flag (eg:-ObjC、-lc++等)?

一、為什麼要在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++檔案進行定性分類