xcode4中框架和静态库之间的区别以及如何调用它们 [英] Difference between framework and static library in xcode4, and how to call them

查看:91
本文介绍了xcode4中框架和静态库之间的区别以及如何调用它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对xcode和Objective-c还是很陌生.我想问一个非常基本的问题.

I am quite new to xcode and objective-c. I want to ask a very basic question.

我看到在项目设置中将二进制文件链接到库"时,存在从工作空间中其他项目导入的框架和库的差异.

I saw that when "linking binary to libraries" in project settings, there are differences about framework and libraries, imported from other projects in workspace.

第一个问题,为什么要有一个框架?为什么有图书馆?我的图书馆不能成为框架吗?

First question, why is there a framework? And why is there a library? Cannot my library be a framework?

然后,从 .h 文件中,如何从导入的静态库中调用类?

And then, from a .h file, how can I call classes from my imported static library?

我想必须有一个前缀,但是找不到. "ProjName/Myclass.h"也不起作用.

I suppose there must be a prefix, but I couldn't find it. Neither nor "ProjName/Myclass.h" are working.

请,请尽可能具体.

谢谢

推荐答案

框架相对于静态库的最大优点是,它们充当打包已编译的库二进制文件和任何相关标头的巧妙方式.可以将它们放到您的项目中(就像SDK的内置框架,如Foundation和UIKit一样),它们应该可以正常工作(大部分时间).

The biggest advantage a framework has over static libraries is that they act as a neat way of packaging up the compiled library binary and any related headers. They can be dropped into your project (just like the SDK's built-in frameworks like Foundation and UIKit) and they should just work (most of the time).

大多数框架都包含动态库;使用Mac Framework模板在Xcode中创建的框架将创建一个动态库. iPhone不支持动态框架,这就是为什么将iOS代码的可重用库分配为静态库的原因.

Most frameworks contain dynamic libraries; frameworks created in Xcode using the Mac Framework template will create a dynamic library. The iPhone does not support dynamic frameworks which is why it has become common for reusable libraries of iOS code to be distributed as static libraries instead.

静态库很好,但是用户需要一些额外的工作.您需要将项目链接到库,并且需要将头文件复制到项目中在构建设置中设置适当的头搜索路径来引用它们.

Static libraries are fine, but they require a bit of extra work on the part of the user. You need to link your project to the library and you need to copy the header files into your project or reference them somewhere by setting the appropriate header search paths in your build settings.

因此:总而言之,我的观点是,分发库的最佳方法是作为一个框架.要为iOS创建一个静态"框架,您基本上可以采用一个普通框架并将二进制文件替换为已编译的静态库.这就是我分发我的其中一个库 Resty 的方式,也是将来打算分发我的库的方式.

So: in summary, my opinion is that the best way of distributing your library is as a framework. To create a "static" framework for iOS, you can essentially take a normal framework and replace the binary with your compiled static library. This is how I distribute one of my libraries, Resty and is how I intend to distribute my libraries in the future.

您可能希望查看该项目中提供的Rakefile(以防万一,Rake与Ruby的Make等效).我有一些任务来编译我的项目(使用xcodebuild)并将其打包为iOS的静态框架.您应该发现这很有用.

You may want to look at the supplied Rakefile in that project (in case you aren't aware, Rake is Ruby's equivalent of Make). I have a handful of tasks for compiling my project (using xcodebuild) and packaging them as a static framework for iOS. You should find this useful.

或者,您可能希望使用这些Xcode 4模板来创建iOS框架.

Alternatively, you may wish to use these Xcode 4 templates for creating an iOS framework.

更新2013年12月9日:这是一个很受欢迎的答案,所以我想编辑一遍,说我分配图书馆的第一选择已经改变.对于任何第三方库,无论是作为消费者还是生产者,我的第一选择都是CocoaPods.我使用CocoaPods分发我的库,并提供一个预编译的静态库,其中包含标头作为后备选项.

Update 9 Dec 2013: this is a popular answer so I thought I'd edit to say that my first choice for library distribution has changed. My first choice for any third party library as either a consumer or producer is CocoaPods. I distribute my libraries using CocoaPods and offer a precompiled static library with headers as a fallback option.

这篇关于xcode4中框架和静态库之间的区别以及如何调用它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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