xcode 安装自定义可可框架的最佳方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1675307/
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
Best way to install a custom cocoa framework
提问by Elise van Looij
I have a custom framework that, following the advice in Apple's Framework Programming Guide >> Installing your frameworkI install in /Library/Frameworks. I do this by adding a Run Script build phase with the following script:
我有一个自定义框架,按照 Apple 框架编程指南中的建议>>安装我安装在 /Library/Frameworks 中的框架。我通过使用以下脚本添加运行脚本构建阶段来做到这一点:
cp -R build/Debug/MyFramework.framework /Library/Frameworks
In my projects I then link against /Library/Frameworks/MyFramework and import it in my classes like so:
在我的项目中,我然后链接到 /Library/Frameworks/MyFramework 并将其导入到我的类中,如下所示:
#import <MyFramework/MyFramework.h>
This works very well, except that I always see the following message in my debugger console:
这非常有效,只是我总是在调试器控制台中看到以下消息:
Loading program into debugger… sharedlibrary apply-load-rules all warning: Unable to read symbols for "/Users/elisevanlooij/Library/Frameworks/MyFramework.framework/Versions/A/MyFramework" (file not found). warning: Unable to read symbols from "MyFramework" (not yet mapped into memory). Program loaded.
正在将程序加载到调试器中…… sharedlibrary apply-load-rules all 警告:无法读取“/Users/elisevanlooij/Library/Frameworks/MyFramework.framework/Versions/A/MyFramework”的符号(未找到文件)。警告:无法从“MyFramework”中读取符号(尚未映射到内存中)。程序加载。
Apparently, the compiler first looks in /Users/elisevanlooij/Library/Frameworks, can't find MyFramework, then looks in /Library/Frameworks, does find MyFramework and continues on its merry way. So far this has been more of an annoyance than a real problem, but when runnning unit tests, gdb stops on the (file not found) and refuses to continue. I have solved the problem by adding an extra line to the Run Script Phase
显然,编译器首先在 /Users/elisevanlooij/Library/Frameworks 中查找,找不到 MyFramework,然后在 /Library/Frameworks 中查找,确实找到了 MyFramework 并继续其愉快的方式。到目前为止,这比真正的问题更令人烦恼,但是在运行单元测试时,gdb 在(找不到文件)上停止并拒绝继续。我已经通过向运行脚本阶段添加额外的行解决了这个问题
cp -R build/Debug/MyFramework.framework ~/Library/Frameworks
but it feels like sello-taping something that shouldn't be broken in the first place. How can I fix this?
但感觉就像在录制不应该被破坏的东西。我怎样才能解决这个问题?
回答by Elise van Looij
In the past months, I've learned a lot more about frameworks, so I'm rewriting this answer. Please note that I'm talking about installing a framework as part of the development workflow.
在过去的几个月里,我学到了更多关于框架的知识,所以我正在重写这个答案。请注意,我说的是将框架安装为开发工作流程的一部分。
The preferred location for installing a public framework (i.e. a framework that will be used by more than one of your apps or bundles) is /Library/Frameworks[link text] because "frameworks in this location are discovered automatically by the compiler at compile time and the dynamic linker at runtime."[Framework Programming Guide]. The most elegant way to do this is in the Deployment section of the Build settings.
安装公共框架(即一个将被多个应用程序或捆绑包使用的框架)的首选位置是 /Library/Frameworks[link text] 因为“编译器在编译时会自动发现此位置的框架和运行时的动态链接器。”[框架编程指南]。最优雅的方法是在构建设置的部署部分。
As you work on your framework, there are times when you do want to update the framework when you do a build, and times when you don't. For that reason, I change the Deployment settings only in the Release Configuration. So:
在您处理框架时,有时您确实想在构建时更新框架,有时又不想。因此,我只在发布配置中更改部署设置。所以:
- Double-click on the framework target to bring up the Target info window and switch to the Build tab.
- Select Release in the Configuration selectbox.
- Scroll down to the Deployment section and enter the following values:
- 双击框架目标以打开目标信息窗口并切换到构建选项卡。
- 在配置选择框中选择发布。
- 向下滚动到部署部分并输入以下值:
Deployment Location = YES (click the checkbox)
Installation Build Products Location = /
Installation Directory = /Library/Frameworks
部署位置 = YES(单击复选框)
安装构建产品位置 = /
安装目录 = /Library/Frameworks
The Installation Build Products Location serves as the root of the installation. Its default value is some /tmp directory: if you don't change it to the system root, you'll never see your installed framework since it's hiding in the /tmp.
Installation Build Products Location 作为安装的根目录。它的默认值是某个 /tmp 目录:如果您不将其更改为系统根目录,您将永远不会看到已安装的框架,因为它隐藏在 /tmp 中。
Now you can work on your framework as you like in the Debug configuration without upsetting your other projects and when you are ready to publish all you need to do is switch to Release and do a Build.
现在,您可以在 Debug 配置中随意处理您的框架,而不会打扰您的其他项目,当您准备好发布时,您需要做的就是切换到 Release 并进行构建。
Xcode 4 WarningSince switching to Xcode 4, I've experienced a number of problems with my custom framework. Mostly, they are linking warnings in GDB that do not really interfere with the usefulness of the framework, except when running the built-in unit-test. I have submitted a technical support ticket to Apple a week ago, and they are still looking into it. When I get a working solution I will update this answer since the question has proven quite popular (1 kViews and counting).
Xcode 4 警告自从切换到 Xcode 4 后,我的自定义框架遇到了许多问题。大多数情况下,它们链接 GDB 中的警告,这些警告不会真正干扰框架的有用性,除非在运行内置单元测试时。一周前我已经向 Apple 提交了技术支持票,他们仍在调查中。当我得到一个可行的解决方案时,我会更新这个答案,因为这个问题已经证明非常流行(1 kViews 和计数)。
回答by Peter Hosey
There's not much reason to put a framework into Library/Frameworks, and it's a lot of work: You'd need to either do it for the user in an Installer package, which is a tremendous hassle to create and maintain, or have installation code in your app (which could only install to ~/L/F, unless you expend the time and effort necessary to make your app capable of installing to /L/F with root powers).
没有太多理由将框架放入库/框架中,而且工作量很大:您要么需要在安装程序包中为用户执行此操作,这对于创建和维护来说是一个巨大的麻烦,或者有安装代码在您的应用程序中(只能安装到 ~/L/F,除非您花费必要的时间和精力使您的应用程序能够使用 root 权限安装到 /L/F)。
Much more common is what Apple calls a “private framework”. You'll bundle this into your application bundle.
更常见的是Apple 所说的“私有框架”。您将把它捆绑到您的应用程序包中。
Even frameworks intended for general use by any applications (e.g., Sparkle, Growl) are, in practice, built to be used as private frameworks, simply because the “right” way of installing a single copy of the framework to Library/Frameworks is such a hassle.
即使是供任何应用程序(例如 Sparkle、Growl)普遍使用的框架,在实践中也被构建为用作私有框架,仅仅是因为将框架的单个副本安装到 Library/Frameworks 的“正确”方式是这样的麻烦。
回答by cdespinosa
The conventional way to do this is to have your framework project and its clients share a common build directory. Xcode will search for framework headers and link against framework binaries in the build folder first, before any other location. So an app project that compiles and links against the header will pick up the most-recently-built one, rather than whatever's installed.
执行此操作的常规方法是让您的框架项目及其客户端共享一个公共构建目录。Xcode中会搜索框架头和反对build文件夹框架的二进制文件的链接首先,任何其他位置之前。因此,编译和链接头文件的应用程序项目将选择最近构建的项目,而不是安装的任何项目。
You can then remove the cp -r and instead use the Install Location build setting to place your build product in the final location, using xcodebuild install DSTROOT=/ at the command line. But you'll only need to do this when you're finished, not every time you rebuild the framework.
然后,您可以删除 cp -r 并使用安装位置构建设置将构建产品放置在最终位置,在命令行使用 xcodebuild install DSTROOT=/ 。但是您只需要在完成后执行此操作,而不是每次重新构建框架时都需要执行此操作。
回答by Darren
Naturally, when you distribute your framework it should be installed in /Library/Frameworks; however it seems odd to me that you're doing that with the test/debug versions of your framework.
当然,当你分发你的框架时,它应该安装在 /Library/Frameworks 中;但是对我来说,您使用框架的测试/调试版本这样做似乎很奇怪。
My first instinct would be to install test versions under ~/Library, as it just makes setting up your test and debug environment that much simpler. If possible, I would expect the debug/test framework to be located in the build tree of the version I'm testing, in which case it's installed as a Private Frameworkfor testing purposes. That would make your life much simpler when it comes time to deal with multiple versions of your framework.
我的第一直觉是在 ~/Library 下安装测试版本,因为它只会让设置测试和调试环境变得更加简单。如果可能,我希望调试/测试框架位于我正在测试的版本的构建树中,在这种情况下,它作为私有框架安装以进行测试。当需要处理框架的多个版本时,这将使您的生活变得更加简单。
Ultimately, it doesn't matter where the framework is located as long as your application or test suite loads the correct version. Choose the location that makes testing/debugging/development easiest.
最终,只要您的应用程序或测试套件加载了正确的版本,框架位于何处并不重要。选择使测试/调试/开发最容易的位置。