第一种方法:直接新建一个工程,具体操作请参照博客
第二种方法:在原有工程上操作,打开已存在的工程,然后添加一个要做成.a静态库的 Target在此,简单命名为mylibrary
此处可以看到,新建的Target 里面同时具有 mylibrary.h
然后就是怎么弄自己的静态库了,你有一些.h
然后选择 静态库
同时 Edit Sheme
然后
此时表明一个静态库已经打出来了
注意事项:
在开发过程中,经常会碰到一些在不同工程中经常用到的部分,把这些部分抽取出来做成一个静态库往往是一个比较好的做法。xcode里就有制作静态库的模板,相关的制作步骤网上也有很多,但在实际的操作中,还是有不少细节方面需要注意。以下是我碰到的一些问题总结。
1.编译release版本的库
在“Manage Schemes”中,将“Build Configuration”的选项改为“Release”即可。如图:
2.静态库中包含category
如果你在静态库工程中使用了category,那么你可能会碰到链接问题,解决的办法就是需要同时在生成静态库的工程和使用静态库的工程中使用“-all_load”编译选项,即在对应target的"BuildSettings"中的“Other LinkerFlags”选项添加“-all_load”。注意:使用静态库的工程中是一定要加该编译选项的!!至于生成静态库的工程中加不加没有试过,不过建议还是加上该编译选项。
3.静态库支持的SDK版本
为了使自己的静态库尽可能多的支持IOS的系统版本,应该在"IOS DeploymentTarget"这个选项中选择自己所需的IOS版本。设置如下图,这个是我的静态库工程中的配置,红框框起来的是我修改过的选项。
4.自动拷贝头文件
在工程对应的target的“Build Phases”下添加“CopyHeaders”的选项。该选项默认是没有的,添加方法是点击下方的“Add BuildPhase”按钮后选择后即可添加。该选项下有3个子选项,分别是Public,Private,Project。通过点击下方的加号,可以将工程中的头文件添加到“Project”中,在其中的对应头文件点击右键,选择“Moveto PublicGroup”,当头文件移到“Pulic”后,编译工程以后,在工程编译后.a文件所在的路径下,会同时出现一个"usr/local/include"的文件夹,其中的头文件就是publicgroup中的头文件。这时只需将.a文件和这个路径下的头文件拷贝到所需工程文件即可。
Public: The interface is finalized and meant to be used by your product’s clients. A public header is included in the product as readable source code without restriction.
Private: The interface isn’t intended for your clients or it’s in early stages of development. A private header is included in the product, but it’s marked “private”. Thus the symbols are visible to all clients, but clients should understand that they're not
supposed to use them.
Project: The interface is for use only by implementation files in the current project. A project header is not included in the target, except in object code. The symbols are not visible to clients at all, only to you.
本文整理自:http://www.cnblogs.com/superhappy/archive/2013/05/27/3102629.html,http://webfrogs.me/2012/12/18/ios-static-lib/