我如何构建一个Objective-C静态库来作为单个二进制文件和头文件分发? [英] How can I build an Objective-C static library to distribute as a single binary and header file?

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

问题描述

我为Objective-C中的iOS构建一个静态库 MyLibrary ,它将十几个有用的类捆绑在一起,每个类都有自己的.h文件。我想分发 MyLibrary 作为单个编译二进制文件, libMyLibrary.a 和一个.h头文件, MyLibraryAPI.h MyLibraryAPI.h 有一打 #import 语句,每个语句 MyLibrary 的十几个公共课程。希望在其主机项目中包含 MyLibrary 的开发人员只需包含 libMyLibrary.a 二进制文件和 MyLibraryAPI.h 标头。这是目标。



因此,我已经在角色 > MyLibrary Xcode项目成功使用Xcode命令行构建成功 Public 并构建 libMyLibrary.a utils和 lipo 。然后,我在一个主机项目中手动地包含了所有打印的 MyLibrary 头文件和 libMyLibrary.a ,并且主机项目可以使用public MyLibrary 类没有问题。真棒!



问题是如果我删除那些打头文件并使用 MyLibraryAPI.h 而不是目标),宿主项目的类不能再找到 MyLibraryAPI.h 中引用的 MyLibrary 头文件。相反,在编译时,我得到如下错误: MyAwesomeThingDelegate.h:没有这样的文件或目录... 对于每个 MyLibrary MyLibraryAPI.h 中尝试 #import 的类。我在我的主机项目根目录中有一个名为 lib 的文件夹,并且在主机项目构建设置中设置了递归头搜索路径为 lib / ** 并且在图书馆搜索路径中,设置递归路径为 lib / **



我很想听听来自社区的关于如何正确设置主机项目搜索路径的建议,这样我只需要包含 libMyLibrary.a MyLibraryAPI.h 来使用 MyLibrary 类。或者如果我做错了什么,我很乐意听到另一个建议,以实现分发单个二进制和单个API头文件的目标。 方案

我遇到了同样的挑战,并且提供了以下解决方案:首先,我尝试隐藏许多实现细节尽可能。出于这个原因,我通常会建立成对的类:一类是公共接口,另一类是私有实现。公共类只有一个成员实例:指向实现的指针。私人类只有一个前向声明。

  @class MyViewImpl; 

@interface MyView:UIView
{
@private
MyViewImpl * _internal;
}

@property(nonatomic,assign)CGRect highlightArea;
- (void)startAnimation;

@end

其次,我将所有公共声明放入单个头文件。与单独的头文件一样,使用起来也很舒服,但它起作用。剩下的唯一进口是iOS的东西,比如:

  #import< UIKit / UIKit.h> 


I'm building a static library, MyLibrary, for iOS in Objective-C that bundles together a dozen useful classes, each with its own .h file. I'd like to distribute MyLibrary as a single compiled binary, libMyLibrary.a, and a single .h header file, MyLibraryAPI.h. MyLibraryAPI.h has a dozen #import statements, one for each of MyLibrary's dozen public classes. Developers who want to include MyLibrary in their host projects should only have to include the libMyLibrary.a binary and the MyLibraryAPI.h header. This is the goal.

So I have set the Role of each public class in the MyLibrary Xcode project to Public and built libMyLibrary.a successfully using Xcode command line build utils and lipo. Then, I manually included all of the dozen MyLibrary header files along with libMyLibrary.a in a host project, and the host project can use the public MyLibrary classes with no problem. Awesome!

The problem is if I remove those dozen header files and use MyLibraryAPI.h instead (as is my goal), the host project's classes can no longer find the MyLibrary header files referenced in MyLibraryAPI.h. Instead, at compile time, I get errors like: MyAwesomeThingDelegate.h: No such file or directory... for each MyLibrary class that I try to #import in MyLibraryAPI.h. I have a folder in my host project root directory called lib and in host project build settings have set the recursive header search path to lib/** and in Library Search Path, set a recursive path to lib/**.

I'd love to hear suggestions from the community on how to correctly set the host project's search paths so that I only need to include libMyLibrary.a and MyLibraryAPI.h to use the MyLibrary classes. Or if I'm doing something wrong, I'd love to hear another suggestion to achieve my goal of distributing a single binary and a single API header file.

解决方案

I've had the same challenge and I've come with the following solution:

First, I tried to hide as many implementation details as possible. For that reason, I usually built pairs of classes: one class is the public interface and the other one the private implementation. The public class has only one member instance: the pointer to the implementation. The private class has just a forward declaration.

@class MyViewImpl;

@interface MyView : UIView
{
    @private
    MyViewImpl* _internal;
}

@property (nonatomic, assign) CGRect highlightArea;
- (void) startAnimation;

@end

Second, I put all the public declarations into a single header file. It's as comfortable to work with as with separate header file, but it works. The only imports that are left, are import of iOS stuff, such as:

#import <UIKit/UIKit.h>

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

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