美文网首页
合并静态库(转)

合并静态库(转)

作者: 暧暧 | 来源:发表于2015-10-03 16:18 被阅读120次

原文 :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)拖入项目中。即可。这时候,你的静态库,可以使用真机+模拟器。

相关文章

网友评论

      本文标题:合并静态库(转)

      本文链接:https://www.haomeiwen.com/subject/qfrmcttx.html