如何构建一个Objective-C静态库? [英] How to build an Objective-C static library?

查看:182
本文介绍了如何构建一个Objective-C静态库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些Objective-C类,我目前在Cocoa应用程序(Mac OS X)和Cocoa-Touch应用程序(iOS)中都使用它。目前,当我更新这些类,我必须将这些更新的 .h .m 文件复制到这两个项目。



所以,我想把这些类构建成一个Objective-C静态库。然后在所有其他项目中链接到该库。



有没有办法构建一个静态库,使其适用于Cocoa和可可触摸应用程序?



我试过只是构建一个Cocoa静态库与这些类,但我得到各种错误时,我试图链接到该库在我的Cocoa应用程序,我确信他们来自的事实,我不是



那么,如何使用 right 方法构建一个Objective-C静态库?我必须构建一个Cocoa-Touch和Cocoa静态库吗?或者我可以只做一个吗?这是怎么做的?



这个主题有没有完善的教程?



我已经在网上搜索,似乎没有找到一个关于这个问题的综合教程,所以我在这里提出了问题,希望一个伟大的答案将被张贴这个问题可以作为我和未来问题的资源。

解决方案

对于XCode 5.这些答案似乎有点的日期。
您可以在这里查看主要步骤





6.现在需要构建每个架构(模拟器和iOS设备)



6.2选择我们的库,然后选择iOS设备,单击生成或运行(Command + B)



6.3.然后对于模拟器(选择Xcode的左上角的库图标,然后选择iOS模拟器)



7.您现在想要构建BullsEye图标> iOS设备。这将是聚合目标,并将我们的两个库合并到一个新的通用文件。



8.让看看这是否都工作。
展开左侧树中的Products文件夹,右键单击以.a和在Finder中显示结尾的文件



这一切工作,你应该在finder中看到以下文件。
您将看到



10.最后一步很简单,只需将library.a文件和includes文件夹拖放到新的Xcode项目中即可。





在您的新项目中,您不必执行任何其他操作,除了导入头文件想要使用,但你不必改变链接标志或标题搜索路径,它只是工作。



(任何反馈赞赏)或请编辑我的帖子以提高清晰度。



**奖励点***
安装VVDocumenter https://github.com/onevcat/VVDocumenter-Xcode ,并使用此文档记录您将与库共享的任何header.h文件。



现在任何使用你的库的人都可以选择+点击来为你的类提供上下文感知帮助。




I have some Objective-C classes, which I am currently using in both a Cocoa application (Mac OS X) and a Cocoa-Touch application (iOS). Currently, when I update those classes, I have to copy those updated .h and .m files to both projects. Not that big of deal, but I'm going to be using them in many more projects.

So, I want to build these classes into an Objective-C static library. And then link against that library in all other projects.

Is there a way to build a static library such that it works with both Cocoa and cocoa-touch applications?

I've tried just building a Cocoa static library with these classes, but I get various errors when I try to link against that library in my Cocoa application, and I'm sure that they are coming from the fact that I am not doing it right.

So, how do I build an Objective-C static library the right way? Do I have to build both a Cocoa-Touch and Cocoa static library? Or can I just do one? How is this done?

Are there any well-done tutorials on this subject?

I have searched the web and cannot seem to find a comprehensive tutorial on this subject, so I've posed the question here, in hopes that a great answer will be posted and this question can serve as a resource for me and for future questioners.

解决方案

For XCode 5. These answers seem a bit out of date. You can see the main steps here http://www.raywenderlich.com/41377/creating-a-status-library-in-ios-tutorial . But Xcode 5 does a lot more work for you and now works nearly as you want it to.

1.Create new Static Library App in Xcode

2.You can delete any files it creates and add your own. Add your methods etc.

3.Little problem with Static Libraries, is that you can only build either for the device or run in the simulator. So we need to create a library that supports ARM and i386 Architectures, to do this we need to create a universal binary (so it will support simulator and on device builds).

Create a new target (File > New > Target).

4.Select New Target then add a new build phase (Editor > Add Build Phase > Add Run Script Build Phase

5.Select the build phase and enter the script from this link into run script window. https://gist.github.com/sponno/7228256

6.Now you need to build each architecture (simulator and iOS device)

6.2.Select our libary and then iOS Device, click build or run (Command + B)

6.3.Then for the Simulator (select the Library Icon very top left of Xcode and then the iOS Simulator)

7.Now you want to build the BullsEye icon > iOS Device. This will be the aggregate target and will combine our two libraries into a new universal file.

8.Lets see if this all worked. Expanded the folder "Products" in the left tree, right click on the file ending with ".a" and "Show in Finder"

9.If this all works you should see the following files in finder. You will see the

10.The last step is easy, simply drag the "library.a file and the includes folder into your new Xcode project.

In your new project, you wont have to do anything else, other than import the header files that want to use, but you do not have to change the linking flags, or header search paths. It will just work.

(any feedback appreciated) or please edit my post to improve clarity.

** BONUS POINTS *** Install VVDocumenter https://github.com/onevcat/VVDocumenter-Xcode and use this to document any header.h files that you will share with the library.

Now anyone that is using your library can opt+click to bring up context aware help for you classes.

这篇关于如何构建一个Objective-C静态库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆