Objective - C静态库及其公共标题 - 什么是正确的方法?

时间:2022-12-25 15:04:40

I'm building a static library that will be used in multiple iOS apps. In parallel i'm working on one of those apps using my library.

我正在构建一个将在多个iOS应用程序中使用的静态库。与此同时,我正在使用我的库处理其中一个应用程序。

During development I get at least once a day an annoying error about header files from the library not being found (in my app project). I learned that when building a static library, headers can be either Public, Private or Project

在开发过程中,我每天至少会遇到一个令人烦恼的错误,即有关未找到库的头文件(在我的应用程序项目中)。我了解到,在构建静态库时,标题可以是Public,Private或Project

I'm guessing that every header that I want to expose in my library should be Public.

我猜我想要在我的库中公开的每个标题都应该是Public。

My question is, what is the best way to manage these public headers? should I create one main public header file with #import to all my public headers? Can Xcode generate such file for me?

我的问题是,管理这些公共标题的最佳方法是什么?我应该使用#import为我的所有公共标题创建一个主公共头文件吗? Xcode可以为我生成这样的文件吗?

Another major question is what is the recommended value for Public Header Folder Path setting?

另一个主要问题是Public Header Folder Path设置的推荐值是多少?

My major goal is that future projects that will use this library, will be able to do so with as less configurations as possible (Adding linker flags, changing User Header Search Path etc.)

我的主要目标是将来使用此库的项目将能够使用尽可能少的配置(添加链接器标志,更改用户标头搜索路径等)。

Thank you very much.

非常感谢你。

1 个解决方案

#1


-1  

About public/private/project headers, here is a previous answer just in case => https://*.com/a/8016333/1075192

关于公共/私人/项目标题,以下是以前的答案以防万一=> https://*.com/a/8016333/1075192

Most of the time, C/C++/Obj-C libraries and frameworks have one main header including all the other ones. Cocoa frameworks follow this path for example.

大多数情况下,C / C ++ / Obj-C库和框架都有一个主标题,包括所有其他标题。例如,Cocoa框架遵循此路径。

To declare a CAAnimation with QuartzCore framework for example you could do this:

例如,要使用QuartzCore框架声明CAAnimation,您可以这样做:

#import "QuartzCore/CAAnimation.h"

or

#import "QuartzCore/Quartzcore.h"

"QuartzCore.h" is actually only composed of a header guard and includes of all the other headers of the library.

“QuartzCore.h”实际上只包含一个标题保护,并包含该库的所有其他标题。

In my opinion, It's actually better to use the first one as it doesn't include all the declarations you don't need. But most of the time we choose the lazy solution of including everything at once.

在我看来,使用第一个实际上更好,因为它不包括你不需要的所有声明。但大多数时候我们选择一次性包含所有内容的懒惰解决方案。

I have no idea if you can generate this directly with xCode, anyway it's easy enough to do it by yourself as it's just a bunch of includes or imports.

我不知道你是否可以直接使用xCode生成这个,无论如何它很容易自己做,因为它只是一堆包含或导入。

The public header folder path is generally "MyLibraryOrFramework/Headers" (just look inside any Cocoa's framework for example).

公共头文件夹路径通常是“MyLibraryOrFramework / Headers”(例如,只需查看任何Cocoa的框架)。

And last, if you want something really reusable and seamless, I suggest you use frameworks instead of static library (which is nearly similar).

最后,如果你想要一些真正可重用且无缝的东西,我建议你使用框架而不是静态库(几乎相似)。

Look at this fine answer to understand why it fits perfectly what you need: => https://*.com/a/6389802/1075192

看看这个很好的答案,了解为什么它完全符合你的需要:=> https://*.com/a/6389802/1075192

Voilà, hope this answer helped a bit.

Voilà,希望这个答案有所帮助。

#1


-1  

About public/private/project headers, here is a previous answer just in case => https://*.com/a/8016333/1075192

关于公共/私人/项目标题,以下是以前的答案以防万一=> https://*.com/a/8016333/1075192

Most of the time, C/C++/Obj-C libraries and frameworks have one main header including all the other ones. Cocoa frameworks follow this path for example.

大多数情况下,C / C ++ / Obj-C库和框架都有一个主标题,包括所有其他标题。例如,Cocoa框架遵循此路径。

To declare a CAAnimation with QuartzCore framework for example you could do this:

例如,要使用QuartzCore框架声明CAAnimation,您可以这样做:

#import "QuartzCore/CAAnimation.h"

or

#import "QuartzCore/Quartzcore.h"

"QuartzCore.h" is actually only composed of a header guard and includes of all the other headers of the library.

“QuartzCore.h”实际上只包含一个标题保护,并包含该库的所有其他标题。

In my opinion, It's actually better to use the first one as it doesn't include all the declarations you don't need. But most of the time we choose the lazy solution of including everything at once.

在我看来,使用第一个实际上更好,因为它不包括你不需要的所有声明。但大多数时候我们选择一次性包含所有内容的懒惰解决方案。

I have no idea if you can generate this directly with xCode, anyway it's easy enough to do it by yourself as it's just a bunch of includes or imports.

我不知道你是否可以直接使用xCode生成这个,无论如何它很容易自己做,因为它只是一堆包含或导入。

The public header folder path is generally "MyLibraryOrFramework/Headers" (just look inside any Cocoa's framework for example).

公共头文件夹路径通常是“MyLibraryOrFramework / Headers”(例如,只需查看任何Cocoa的框架)。

And last, if you want something really reusable and seamless, I suggest you use frameworks instead of static library (which is nearly similar).

最后,如果你想要一些真正可重用且无缝的东西,我建议你使用框架而不是静态库(几乎相似)。

Look at this fine answer to understand why it fits perfectly what you need: => https://*.com/a/6389802/1075192

看看这个很好的答案,了解为什么它完全符合你的需要:=> https://*.com/a/6389802/1075192

Voilà, hope this answer helped a bit.

Voilà,希望这个答案有所帮助。