xcode 使用私有 dylib/框架构建 Cocoa 应用程序包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8102609/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:23:59  来源:igfitidea点击:

Build Cocoa application Bundle with private dylib/framework

objective-cxcodecocoaframeworksbuild

提问by ianXian

I use xcode 4 to build a cocoa application with a private dylib/framework.

我使用 xcode 4 构建一个带有私有 dylib/框架的可可应用程序。

In my development Mac, I put the dylib in the /usr/local/lib directory, and drag it into the project.

在我的开发Mac中,我把dylib放在/usr/local/lib目录下,拖到项目中。

The app is compiled and runs perfect on my computer.

该应用程序已编译并在我的计算机上完美运行。

To distribute this app to the other Mac, I create a copy Files building phase, and say "copy that dylib to Frameworks directory".

为了将此应用程序分发到另一台 Mac,我创建了一个复制文件构建阶段,并说“将该 dylib 复制到 Frameworks 目录”。

The application is built successfully, and I indeed see the dylib is copied to the Frameworks directory in the app bundle.

应用程序构建成功,我确实看到dylib被复制到应用程序包中的Frameworks目录。

The problem is when I run this app in another regular Mac, which does not have this dylib installed. I get an error saying:

问题是当我在另一台没有安装这个 dylib 的普通 Mac 上运行这个应用程序时。我收到一条错误消息:

dyld: Library not loaded: /usr/local/lib/mylib.dylib

采纳答案by sergio

The issue comes from the fact that you copy the framework into the app bundle, so it is available at a location like:

问题在于您将框架复制到应用程序包中,因此它在以下位置可用:

 <you_app_path>/Contents/Frameworks

but you try to load it from /usr/local/libwhere it is not available on you deployment machine. From Apple Framework Programming Guide:

但是您尝试从/usr/local/lib部署机器上不可用的地方加载它。来自Apple 框架编程指南

To embed a framework in an application, there are several steps you must take:

You must configure the build phases of your application target to put the framework in the correct location.

You must configure the framework target's installation directory, which tells the framework where it will live.

You must configure the application target so that it references the framework in its installation directory.

要将框架嵌入到应用程序中,您必须执行以下几个步骤:

您必须配置应用程序目标的构建阶段以将框架放置在正确的位置。

您必须配置框架目标的安装目录,该目录告诉框架它将存在于何处。

您必须配置应用程序目标,以便它引用其安装目录中的框架。

Now, you say that the build phase is ok; I assume also that you sent the application target build setting correctly. What is left is configuring the framework target's installation directory.

现在,你说构建阶段没问题;我还假设您正确发送了应用程序目标构建设置。剩下的就是配置框架目标的安装目录。

If you did not build the framework yourself, you should be able to fix this by changing the framework install path so that it is defined relative to the loader (your app), to something like: @loader_path/../Frameworks/(or @executable_path/../Frameworks). You can do that by means of install_name_tool.

如果您没有自己构建框架,您应该能够通过更改框架安装路径来解决此问题,以便它相对于加载程序(您的应用程序)进行定义,例如:(@loader_path/../Frameworks/@executable_path/../Frameworks)。您可以通过install_name_tool来做到这一点。

If you are compiling on your own the private framework, you can define its install location in Xcode build settings.

如果您自己编译私有框架,则可以在 Xcode 构建设置中定义其安装位置。