文件后綴名
h :頭文件银还。頭文件包含類,類型腐碱,函數(shù)和常數(shù)的聲明誊垢。
.m :源代碼文件。這是典型的源代碼文件擴(kuò)展名症见,可以包含Objective-C和C代碼彤枢。
.mm :源代碼文件。帶有這種擴(kuò)展名的源代碼文件筒饰,除了可以包含Objective-C和C代碼以外還可以包含C++代碼。僅在你的Objective-C代碼中確實(shí)需要使用C++類或者特性的時(shí)候才用這種擴(kuò)展名
.cpp:只能編譯C++壁晒;
注意:.m 和.mm 的區(qū)別是告訴gcc 在編譯時(shí)要加的一些參數(shù)瓷们。當(dāng)然.mm還可以命名成.m,但在編譯時(shí)要手動(dòng)加參數(shù)(麻煩)。
Framework search path:where to search frameworks (.framework bundles) in addition to system frameworks paths. Not used very much in iOS development, officially there is no developer iOS frameworks.
Header search path:where to search for header files (.h files) in addition to system paths. Usually you’ll need it if you are using a 3rd party library. Set it to the directory where you have the header files. If you use a directory to include the header (example: #import “mylibrary/component.h”) set it to the parent directory.
In xcconfig files you use this variable:
HEADER_SEARCH_PATHS = “/path/to/headers/container/directory
Library search path:where to search for library files in addition to system paths. Xcode will set it automatically if you drag a library (.a files) into the project. To set it manually, use the directory where the library is located.
In xcconfig files you use this variable:
LIBRARY_SEARCH_PATHS = “/path/to/libraries/container/directory”
說明:All three can hold a list of paths, with quotes, separated by space.
該設(shè)置秒咐,用來填寫XCode的鏈接器參數(shù)谬晕,如:-ObjC -all_load -force_load等。(xcode采用的鏈接器為ld–GNU携取,主要用于將obj文件連接成可執(zhí)行文件攒钳。可以通過man ld 命令了解更多)
還記得我們?cè)趯W(xué)習(xí)C程序的時(shí)候雷滋,從C代碼到可執(zhí)行文件經(jīng)歷的步驟是:源代碼 > 預(yù)處理器 > 編譯器 > 匯編器 > 機(jī)器碼 > 鏈接器 > 可執(zhí)行文件
在最后一步需要把.o文件和C語言運(yùn)行庫(kù)鏈接起來不撑,這時(shí)候需要用到ld命令文兢。源文件經(jīng)過一系列處理以后,會(huì)生成對(duì)應(yīng)的.obj文件焕檬,然后一個(gè)項(xiàng)目必然會(huì)有許多.obj文件姆坚,并且這些文件之間會(huì)有各種各樣的聯(lián)系,例如函數(shù)調(diào)用实愚。鏈接器做的事就是把這些目標(biāo)文件和所用的一些庫(kù)鏈接在一起形成一個(gè)完整的可執(zhí)行文件兼呵。
參數(shù)說明:
(在ios開發(fā)過程中,有時(shí)候會(huì)用到第三方的靜態(tài)庫(kù)(.a文件)腊敲,然后導(dǎo)入后發(fā)現(xiàn)編譯正常但運(yùn)行時(shí)會(huì)出現(xiàn)selector not recognized的錯(cuò)誤击喂,從而導(dǎo)致app閃退。閱讀庫(kù)文件的說明文檔碰辅,你可能會(huì)在文檔中發(fā)現(xiàn)諸如在Other Linker Flags中加入-ObjC或者-all_load這樣的解決方法懂昂,原因?)
報(bào)selector not recognized為什么會(huì)閃退:
The"selector not recognized"runtime exception occurs duetoan issuebetweentheimplementationofstandard UNIX static libraries,thelinkerandthedynamic natureofObjective-C. Objective-Cdoesnotdefine linker symbolsforeach function (ormethod,inObjective-C) - instead, linker symbols are only generatedforeachclass. If you extend a pre-existingclasswithcategories,thelinkerdoesnotknowtoassociatetheobject codeofthecoreclassimplementationandthecategory implementation. This prevents objects createdintheresultingapplicationfromrespondingtoa selectorthatisdefinedinthecategory.
1
翻譯過來乎赴,大概意思就是Objective-C的鏈接器并不會(huì)為每個(gè)方法建立符號(hào)表忍法,而是僅僅為類建立了符號(hào)表。這樣的話榕吼,如果靜態(tài)庫(kù)中定義了已存在的一個(gè)類的分類饿序,鏈接器就會(huì)以為這個(gè)類已經(jīng)存在,不會(huì)把分類和核心類的代碼合起來羹蚣。這樣的話原探,在最后的可執(zhí)行文件中,就會(huì)缺少分類里的代碼顽素,這樣函數(shù)調(diào)用就失敗了咽弦。
-ObjC,一般這個(gè)參數(shù)足夠解決前面提到的問題胁出,蘋果官方說明如下:
This flag causesthelinkertoloadeveryobjectfileinthelibrarythatdefines an Objective-Cclassorcategory. While this option will typicallyresultina larger executable (duetoadditional object code loadedintotheapplication),itwill allowthesuccessful creationofeffective Objective-C static librariesthatcontaincategoriesonexisting classes.
1
簡(jiǎn)單說來型型,加了這個(gè)參數(shù)后,鏈接器就會(huì)把靜態(tài)庫(kù)中所有的Objective-C類和分類都加載到最后的可執(zhí)行文件中全蝶,雖然這樣可能會(huì)因?yàn)榧虞d了很多不必要的文件而導(dǎo)致可執(zhí)行文件變大闹蒜,但是這個(gè)參數(shù)很好地解決了我們所遇到的問題。但是事實(shí)真的是這樣的嗎抑淫?
如果-ObjC參數(shù)真的這么有效绷落,那么事情就會(huì)簡(jiǎn)單多了。
Important: For64-bitandiPhone OS applications, thereisa linker bugthatprevents -ObjCfromloading objects filesfromstatic librariesthatcontainonly categoriesandno classes. The workaroundistousethe-allloador-forceload flags.
1
當(dāng)靜態(tài)庫(kù)中只有分類而沒有類的時(shí)候始苇,-ObjC參數(shù)就會(huì)失效了砌烁。這時(shí)候,就需要使用-all_load或者-force_load了催式。
-all_load會(huì)讓鏈接器把所有找到的目標(biāo)文件都加載到可執(zhí)行文件中函喉,但是千萬不要隨便使用這個(gè)參數(shù)避归!假如你使用了不止一個(gè)靜態(tài)庫(kù)文件,然后又使用了這個(gè)參數(shù)函似,那么你很有可能會(huì)遇到ld: duplicate symbol錯(cuò)誤槐脏,因?yàn)椴煌膸?kù)文件里面可能會(huì)有相同的目標(biāo)文件,所以建議在遇到-ObjC失效的情況下使用-force _load參數(shù)撇寞。
-force _load所做的事情跟-all_load其實(shí)是一樣的顿天,但是-force_load需要指定要進(jìn)行全部加載的庫(kù)文件的路徑,這樣的話蔑担,你就只是完全加載了一個(gè)庫(kù)文件牌废,不影響其余庫(kù)文件的按需加載。(-force_load后面需要文檔路徑)