合并靜態庫(轉)

原文 :http://blog.sina.com.cn/s/blog_7b9d64af0101jlym.html

在iOS的開發過程中,我們常常用到第三方的庫。尤其是QQ、百度地圖、廣告等。

那么,如何制作自己的庫文件呢?

如果,將自己寫的功能類編譯成庫文件,分發給其他人來使用呢?

靜態庫的優點

編譯靜態庫的好處也還是有的!

1.讓自己的源碼不被直接暴漏。

2.需要使用時,僅僅拷貝相應的.h文件和.a文件就好,不用在將源碼一一拷貝。方便。

3.顯得也比源碼拷貝高端、大氣一些。

那么,廢話就不多說了!準備動工!

一、建立相應的靜態庫項目

這樣,默認獲得了項目同名的一組.h和.m文件。

熟悉吧,就在相應的文件中,寫入功能函數!在本例子中,簡單的直接寫入相應的2個方法。

MyStaticLibraryDemo.h

文件

#import

@interfaceMyStaticLibraryDemo:NSObject

///加法

-(int)addMethodByFirst:(int)theFirst andSecond:(int)theSecond;

///減法

-(int)SubMethodByFirst:(int)theFirst andSecond:(int)theSecond;

@end

MyStaticLibraryDemo.m

文件

#import"MyStaticLibraryDemo.h"

@implementationMyStaticLibraryDemo

///加法

-(int)addMethodByFirst:(int)theFirst andSecond:(int)theSecond{

return(theFirst+theSecond);

}

///減法

-(int)SubMethodByFirst:(int)theFirst andSecond:(int)theSecond{

return(theFirst-theSecond);

}

@end

要做的,就這么簡單,然后,調試代碼無誤后,就可以進行編譯了!

二、編譯靜態庫文件:XXXX.a

方法一,直接編譯(command+B)。

這時,會發現,libMyStaticLibraryDemo.a生成了!進入相應的編譯目錄,會看到:

ok,有兩個目錄下的文件是我們需要的。

Release-iphoneos:應用于真機的靜態庫文件。

Release-iphonesimulator:應用于模擬器調試的靜態庫文件。

我們需要使用終端命令來看一下生成的相應的.a文件的屬性(測試環境為作者本機環境):

1.Release-iphoneos版本

bogon:~ zhangzhen$cd/Users/zhangzhen/Library/Developer/Xcode/DerivedData/MyStaticLibraryDemo-ciwnhcsbqgclkododazbmbmtdlfp/Build/Products/Release-iphoneos

bogon:Release-iphoneos zhangzhen$lipo

-infolibMyStaticLibraryDemo.a

Architectures in the fat file:libMyStaticLibraryDemo.a are: armv7 armv7sarm64

可見,編譯的可執行的CPU環境為arm7、armv7s、arm64。

2.Release-iphonesimulator版本

bogon:~ zhangzhen$cd/Users/zhangzhen/Library/Developer/Xcode/DerivedData/MyStaticLibraryDemo-ciwnhcsbqgclkododazbmbmtdlfp/Build/Products/Release-iphonesimulator

bogon:Release-iphonesimulator zhangzhen$lipo -infolibMyStaticLibraryDemo.a

Architectures in the fat file:libMyStaticLibraryDemo.a are: i386x86_64

可見,編譯的可執行版本為i386 x86_64

三、使用靜態庫

在你的要使用太靜態庫的項目中導入libMyStaticLibraryDemo.a文件和include文件夾中的相應的所有.h頭文件。

例如,我要在MyLibraryTest項目中,使用我上述編譯好的靜態庫文件。

導入完成后,項目如下:

注意:你在真機調試和模擬器調試的時候,要替換相應的.a文件版本。

在需要使用該靜態庫的地方,導入相應的.h文件。你就可以使用了!

MyStaticLibraryDemo*myLibrary=[[MyStaticLibraryDemoalloc]init];

intresult= [myLibraryaddMethodByFirst:5andSecond:5];

NSLog(@"Result:%d",result);

result=[myLibrarySubMethodByFirst:10andSecond:5];

NSLog(@"Result:%d",result);

當然,你也可以,針對相應的用途來編譯相應的.a靜態庫。

1.選擇Edit

Scheme項:

2.使用Build

Configuration 來編譯相應的用途版本:

這樣,你就可以得到相應用途的靜態庫編譯版本。

如果,你在使用中,很不幸的遇到了以下問題:

ld: warning: ignoring file/Users/XXXX/Documents/MyLibraryTest/MyLibraryTest/MyLibrary/libMyStaticLibraryDemo.a,missing required architecture i386 in

file/Users/XXXX/Documents/MyLibraryTest/MyLibraryTest/MyLibrary/libMyStaticLibraryDemo.a(3 slices)

Undefined symbols for architecture

i386:

"_OBJC_CLASS_$_MyStaticLibraryDemo", referencedfrom:

objc-class-ref in AppDelegate.o

ld: symbol(s) not found for architecture

i386

clang: error: linker command failed with exit code 1 (use -v to see

invocation)

如圖:

那么,我也很不幸的告訴你,你導入錯誤的編譯版本。

以上錯誤,是你的庫文件(.a)為真機版本,你卻用模擬器來調試程序。將調試目標換成真機,即可!

四、合并靜態庫(真機+模擬器)

如果,你的調試需要不斷在真機和模擬器之間切換。那么,制作一個通用的靜態庫.a文件是一個好想法。

這樣,使用該靜態庫文件就可以在真機和模擬器上調試。

制作過程也是非常簡單。動手吧:

1.使用終端合并2個版本。

bogon:~ zhangzhen$lipo -create/所在路徑/Release-iphoneos/libMyStaticLibraryDemo.a/所在路徑/Release-iphonesimulator/libMyStaticLibraryDemo.a-output/Users/zhangzhen/Desktop/libUniversal.a

bogon:~ zhangzhen$

這樣,就可以合并一個通用版本的靜態庫。唯一不爽的,就是體積要大一些。

通用版本大小>=模擬器版本大小+真機版本大小。

2.集成通用靜態庫

我想,不用我介紹太多了,將以上合并的通用版本的靜態庫文件(libUniversal.a)拖入項目中。即可。這時候,你的靜態庫,可以使用真機+模擬器。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容