iOS形成靜態庫方法


在iOS的開發過程中,我們常常用到第三方的庫。尤其是QQ、百度地圖、廣告等。
那麼,如何製作自己的庫文件呢?
如果,將自己寫的功能類編譯成庫文件,分發給其他人來使用呢?

靜態庫的優點

編譯靜態庫的好處也還是有的!
1.讓自己的源碼不被直接暴漏。
2.需要使用時,僅僅拷貝相應的.h文件和.a文件就好,不用在將源碼一一拷貝。方便。
3.顯得也比源碼拷貝高端、大氣一些。

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

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

iOS生成靜態庫方法-iOS集成靜態庫-iOS合併靜態庫

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

iOS生成靜態庫方法-iOS集成靜態庫-iOS合併靜態庫

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

MyStaticLibraryDemo.h 文件

#import


@interface MyStaticLibraryDemo : NSObject


/// 加法

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


/// 減法

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

@end


MyStaticLibraryDemo.m 文件

#import "MyStaticLibraryDemo.h"


@implementation MyStaticLibraryDemo


/// 加法

- (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生成了!進入相應的編譯目錄,會看到:

iOS生成靜態庫方法-iOS集成靜態庫-iOS合併靜態庫

ok,有兩個目錄下的文件是我們需要的。
Release-iphoneos:應用於真機的靜態庫文件。
Release-iphonesimulator:應用於模擬器調試的靜態庫文件。

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

1.Release-iphoneos版本

iOS生成靜態庫方法-iOS集成靜態庫-iOS合併靜態庫



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

bogon:Release-iphoneos zhangzhen$ lipo -info libMyStaticLibraryDemo.a(輸入)

Architectures in the fat file: libMyStaticLibraryDemo.a are: armv7 armv7s arm64 (輸出)

可見,編譯的可執行的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 -info libMyStaticLibraryDemo.a

Architectures in the fat file: libMyStaticLibraryDemo.a are: i386 x86_64 


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


三、使用靜態庫

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

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

導入完成後,項目如下:

iOS生成靜態庫方法-iOS集成靜態庫-iOS合併靜態庫

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

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

MyStaticLibraryDemo *myLibrary=[[MyStaticLibraryDemo alloc] init];

    int result= [myLibrary addMethodByFirst:5 andSecond:5];

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

    

    result=[myLibrary SubMethodByFirst:10 andSecond:5];

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


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

1.選擇Edit Scheme項:

iOS生成靜態庫方法-iOS集成靜態庫-iOS合併靜態庫

2.使用Build Configuration 來編譯相應的用途版本:

iOS生成靜態庫方法-iOS集成靜態庫-iOS合併靜態庫

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

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

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", referenced from:

      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)


如圖:

iOS生成靜態庫方法-iOS集成靜態庫-iOS合併靜態庫


那麼,我也很不幸的告訴你,你導入錯誤的編譯版本。
以上錯誤,是你的庫文件(.a)爲真機版本,你卻用模擬器來調試程序。將調試目標換成真機,即可!

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

如果,你的調試需要不斷在真機和模擬器之間切換。那麼,製作一個通用的靜態庫.a文件是一個好想法。
這樣,使用該靜態庫文件就可以在真機和模擬器上調試。

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

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

iOS生成靜態庫方法-iOS集成靜態庫-iOS合併靜態庫


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

bogon:~ zhangzhen$ 


這樣,就可以合併一個通用版本的靜態庫。唯一不爽的,就是體積要大一些。
通用版本大小>=模擬器版本大小+真機版本大小。

2.集成通用靜態庫

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

總結

至此,iOS製作靜態庫的方法,就介紹到這裏了!至於使用哪一種?如何使用?是否需要合併……那就要具體問題,具體分析了!

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