xcode 目标 - C 静态库及其公共头文件 - 正确的方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10529604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Objective - C Static Library and it's public headers - what is the right way?
提问by Avi Shukron
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.)
我的主要目标是未来将使用该库的项目能够以尽可能少的配置(添加链接器标志、更改User Header Search Path
等)来实现。
Thank you very much.
非常感谢。
采纳答案by nsarno
About public/private/project headers, here is a previous answer just in case => https://stackoverflow.com/a/8016333/1075192
关于公共/私有/项目标头,这里是以前的答案,以防万一=> https://stackoverflow.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://stackoverflow.com/a/6389802/1075192
看看这个很好的答案,了解为什么它完全适合您的需要:=> https://stackoverflow.com/a/6389802/1075192
Voilà, hope this answer helped a bit.
瞧,希望这个答案有所帮助。