Xcode Build Setting-> other linker flags 設置


Xcode Build Setting-> other linker flags 設置

一、Xcode Help 中Other Linker Flags 定義

在Xcode Help 中查找Build Setting 可以找到:

Other Linker Flags (OTHER_LDFLAGS)

Options defined in this setting are passed to invocations of the linker.

意思是:此設置中定義的選項傳遞給鏈接器調用。(設置的選項是鏈接器的參數)

鏈接器的簡單說明:

一個程序從簡單易讀的代碼到可執行文件往往要經歷以下步驟:

源代碼 > 預處理器 > 編譯器 > 匯編器 > 機器碼 > 鏈接器 > 可執行文件

源文件經過一系列處理以后,會生成對應的.obj文件,然后一個項目必然會有許多.obj文件,並且這些文件之間會有各種各樣的聯系,例如函數調用。鏈接器做的事就是把這些目標文件和所用的一些庫鏈接在一起形成一個完整的可執行文件。

了解鏈接器可以參考:

https://baike.baidu.com/item/鏈接器/10853221?fr=aladdin

 二、Other Linker Flags選項

xcode中,在“Targets”選項下有Other Linker Flags選項,可以填寫xcode鏈接器的參數,如:-ObjC-all_load-force_load、-l 等。

xcode采用的鏈接器為ld–GNU,ld是GNU工具鏈中的一個軟件,主要用於將obj文件連接成可執行文件。

在終端中輸入 man ld 可以了解更多,如下圖所示:

三、部分參數作用

這里要說的主要參數是ld工具的參數,也是在Other Linker Flags里常用到的參數。

上面終端下拉可以看到很多參數介紹,常用的有-ObjC,-all_load,-force_load,-l

如圖:

1.-ObjC

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

設置參數-ObjC,鏈接器會把靜態庫中所有的Objective-C類和類別都加載到最后的可執行文件中。

當使用OC寫的靜態類別庫(Objective-C static library that contains categories),在程序編譯鏈接時,如果不在Other Linker Flags中填寫-ObjC,往往會報錯,出現”selector not recognized”。

蘋果官方Q&A上有這么一段話:

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.

翻譯過來,大概意思就是Objective-C的鏈接器並不會為每個方法建立符號表,而是僅僅為類建立了符號表。這樣的話,如果靜態庫中定義了已存在的一個類的分類,鏈接器就會以為這個類已經存在,不會把分類和核心類的代碼合起來。這樣的話,在最后的可執行文件中,就會缺少分類里的代碼,這樣函數調用就失敗了。

還有一種情況是: 添加-ObjC參數,反而報duplicate symbol 錯誤, 如圖所示:

這個錯誤可能是打包的.a靜態庫有問題,之前嘗試各種方法都不行,后來第三方重新打包才可以。

2.-all_load

強制鏈接器加載所有包含非ObjC的目標文檔。all_load會讓鏈接器把所有找到的目標文件都加載到可執行文件中,但是千萬不要隨便使用這個參數!假如你使用了不止一個靜態庫文件,然后又使用了這個參數,那么你很有可能會遇到ld: duplicate symbol錯誤,因為不同的庫文件里面可能會有相同的目標文件,所以建議在遇到-ObjC失效的情況下使用-force_load參數。

3. -force_load

加載指定的目標文檔。-force_load后面需要文檔路徑,如:

-force_load所做的事情跟-all_load其實是一樣的,但是-force_load需要指定要進行全部加載的庫文件的路徑,這樣的話,你就只是完全加載了一個庫文件,不影響其余庫文件的按需加載。

4. -l

 -lx         This option tells the linker to search for libx.dylib or libx.a in the library search path.  If string x is of the form y.o, then that file is searched for in the same places,but without prepending `lib' or appending `.a' or `.dylib' to the filename.

這個選項通知鏈接器根據搜索路徑搜索libx.dylib或libx.a。

5. -w   

Suppress all warning messages   禁止所有警告消息

6. 還有一種是添加$(inherited)

參考:

[整理]Xcode中的$(inherited)的含義

http://blog.csdn.net/azheng51714/article/details/48030621

 

如果我理解錯誤還望大家指正;

 

參考:

http://blog.sina.com.cn/s/blog_4cd8dd130102v47r.html 

http://alloc.sinaapp.com/wp/?p=272 

http://blog.csdn.net/tammy_min/article/details/12854595

http://small.qiang.blog.163.com/blog/static/978493072013112571950/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM