在iOS中使用靜態庫

由於想了解下Xcode中靜態庫的導出導入,翻到這麼一篇文檔:Using Static Libraries in iOS.

小小嚐試了下。

首先新建一個項目,選擇Cocoa Touch Static Library:


創建成功後,產出就是.a靜態文件了:


這時候可以創建或者添加現有代碼,並且指定暴露的頭文件:


最後就是編譯,生成靜態文件。


接着再新建一份項目用來引入剛纔創建的靜態庫。

打開靜態庫所在目錄:


把頭文件和.a文件都拖到新建工程中:


這裏有個注意點(參考此處):如果你是在模擬器上跑,就選擇下面的iphonesimulator目錄下的資源,如果不是就選擇上面的,否則會出現錯誤:


選擇正確的版本,然後設置鏈接選項、引入要使用的頭文件,編譯就可以通過了。


Select the Build Settings tab in your application target editor. Find the “Other Linker Flags” build setting. Add the flag -ObjC to this build setting’s value if it is not already present. This flag will tell the linker to link all Objective-C classes and categories from static libraries into your application, even if the linker can’t tell that they are used. This is needed because Objective-C is a dynamic language and the linker can’t always tell which classes and categories are used by your application code.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章