xcode 在命令行工具中使用框架
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/630911/
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
Using frameworks in a command line tool
提问by Norman
I've built a command-line utility (Foundation tool) in Xcode, using Cocoa. The tool makes use of a 3rd party framework.
我使用 Cocoa 在 Xcode 中构建了一个命令行实用程序(基础工具)。该工具使用了第 3 方框架。
Everything works OK in Xcode, but how do I deploy this program?
在 Xcode 中一切正常,但是我该如何部署这个程序呢?
If I run the app from Finder, it can't find the library because it's looking in ../Frameworks/etc.
Can I statically link in the 3rd party framework?
如果我从 Finder 运行应用程序,它找不到库,因为它正在查找../Frameworks/etc.
我可以在第 3 方框架中静态链接吗?
回答by Barry Wark
Unfortunately, there is no way to bundle a framework with a command-line Utility in OS X and I suspect that the framework you're linking to is expecting to be bundled in the app bundle's Frameworks/ directory. If you have access to the framework source code, you can compile a static library and statically link it to your application (or include the source in your application target directly). If you don't have the source code or you don't want to statically link the library for some reason, there are two remaining options:
不幸的是,无法在 OS X 中将框架与命令行实用程序捆绑在一起,我怀疑您要链接到的框架期望捆绑在应用程序包的 Frameworks/ 目录中。如果您有权访问框架源代码,则可以编译静态库并将其静态链接到您的应用程序(或直接将源代码包含在您的应用程序目标中)。如果您没有源代码或出于某种原因不想静态链接库,还有两个选项:
If you have access to the system-wide /Library/Frameworks folder, you can install the 3rd party framework there. This require that the framework's Installation Path (the
INSTALL_PATH
build setting) be set to/Library/Frameworks
at build time or that you use theinstall_name_tool
to change the frameworks install path to/Library/Frameworks
(if you don't have the framework's source code).Build an application bundle (as if you were building a GUI app) with your command-line utility as the app bundle's executable (i.e. in AppBundle.app/Contents/MacOS/). You can then copy the 3rd party framework to the app bundle's frameworks directory. You can then put the app bundle anywhere you want and create a symbolic link to the command line utility.
如果您有权访问系统范围的 /Library/Frameworks 文件夹,则可以在那里安装第 3 方框架。这要求在构建时将框架的安装路径(
INSTALL_PATH
构建设置)设置为/Library/Frameworks
,或者您使用install_name_tool
将框架安装路径更改为/Library/Frameworks
(如果您没有框架的源代码)。使用命令行实用程序作为应用程序包的可执行文件(即在 AppBundle.app/Contents/MacOS/ 中)构建应用程序包(就像构建 GUI 应用程序一样)。然后,您可以将第 3 方框架复制到应用程序包的框架目录中。然后,您可以将应用程序包放在您想要的任何位置,并创建一个指向命令行实用程序的符号链接。
Option 1 is definitely the more accepted approach, but I've used option 2 when there was a valid reason.
选项 1 绝对是更被接受的方法,但我在有正当理由时使用了选项 2。
You can find more information on building, linking, and installing frameworks in Apple's Frameworks Programming Guide.
您可以在 Apple 的框架编程指南 中找到有关构建、链接和安装框架的更多信息。
回答by Peter Hosey
Another way, if you have the source code for the framework, is to add a static library target and build a static lib from it. Then you can statically link it into your command-line tool.
另一种方法是,如果您有框架的源代码,则添加一个静态库目标并从中构建一个静态库。然后您可以将其静态链接到您的命令行工具中。
回答by mcm
As of Xcode 9.3.1, I was able to have the framework added to the command line tool by setting the Mach-O Type
to Static Library
for the framework. Then in the command line target
make sure to add the framework to the Target Dependencies
& the Link Binary With Libraries
Build Phases
. The built executable was then able to run with no issues.
从 Xcode 9.3.1 开始,我可以通过为框架设置Mach-O Type
toStatic Library
来将框架添加到命令行工具中。然后在命令行中target
确保将框架添加到Target Dependencies
& 中Link Binary With Libraries
Build Phases
。然后构建的可执行文件能够毫无问题地运行。
回答by johndpope
You can use marathon to manage dependencies https://github.com/JohnSundell/Marathon
您可以使用马拉松来管理依赖项 https://github.com/JohnSundell/Marathon
This would need more thought if you wanted to distribute app. (You would probably want to install into frameworks folder in that use case.) your mileage may vary with this solution.
如果您想分发应用程序,则需要更多考虑。(在该用例中,您可能希望安装到 frameworks 文件夹中。)您的里程可能因此解决方案而异。