xcode OSX:更改 .framework 的路径

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

OSX: changing path of .framework

xcodemacos

提问by psychotik

My Mac OS application links with a non-system-provided framework Foo.framework. I added a reference to the framework in XCode and the app builds fine. I also have a rule which copies the framework into the output Frameworks folder (MyApp.app/Contents/Frameworks). However, at runtime the binary is looking for the framework in ~/Library/Frameworks and the app fails to load.

我的 Mac OS 应用程序链接到非系统提供的框架 Foo.framework。我在 XCode 中添加了对框架的引用,应用程序构建良好。我还有一个将框架复制到输出框架文件夹 (MyApp.app/Contents/Frameworks) 的规则。但是,在运行时,二进制文件会在 ~/Library/Frameworks 中查找框架,并且应用程序无法加载。

otool -l MyApp.app also tells me that it's looking for the framework in /Users//Library/Frameworks.

otool -l MyApp.app 还告诉我它正在 /Users//Library/Frameworks 中寻找框架。

Can someone explain why this happens, and what the right way to make the app look in the application bundle's Frameworks folder is?

有人可以解释为什么会发生这种情况,以及让应用程序在应用程序包的 Frameworks 文件夹中看起来的正确方法是什么?

My hacky workaround is to include a custom script to change the path in the mach-o binary using install_name_tool, but I'm sure there is a clean way of doing this.

我的 hacky 解决方法是包含一个自定义脚本来使用 install_name_tool 更改 mach-o 二进制文件中的路径,但我确信有一种干净的方法可以做到这一点。

回答by wadesworld

You're on the right track.

你在正确的轨道上。

It happens because on OS X an "install_name"is recorded in the library when it's created. That install_name is then copied to any application which links with it.

发生这种情况是因为在 OS X 上"install_name",它在创建时被记录在库中。然后将该 install_name 复制到与其链接的任何应用程序。

The "best" way to solve the problem is to modify the source of the framework so that the -install_namelinkder flag is set when the framework is built.

解决问题的“最佳”方法是修改框架的源代码,以便install_name在构建框架时设置-linkder 标志。

However, that's often not possible, either because you don't have the source, or the framework has a huge mess of autoconf stuff that makes it near impossible. In that case, use install_name_tool with the -id flag to change the recorded install_namein the library.

然而,这通常是不可能的,要么是因为你没有源代码,要么是框架中有一大堆 autoconf 的东西,这几乎是不可能的。在这种情况下,使用带有 -id 标志的 install_name_tool 来更改install_name库中的记录。

So, with all that said, what do you change it to?

那么,说了这么多,你要把它改成什么?

@executable_path/../Frameworks/Foo.framework/Foo (or whatever the name is)

@executable_path/../Frameworks/Foo.framework/Foo(或任何名称)

So, from the directory containing Foo.framework:

因此,从包含 Foo.framework 的目录中:

install_name_tool -id @executable_path/../Frameworks/Foo.framework/Foo Foo.framework/Foo

At runtime, the loader will resolve @executable_path to the path to the application's executable. Obviously you also need to setup a Copy Files build phase to copy the framework into the Framework folder of the application bundle.

在运行时,加载器会将@executable_path 解析为应用程序可执行文件的路径。显然,您还需要设置一个 Copy Files 构建阶段,以将框架复制到应用程序包的 Framework 文件夹中。

You can also use install_name_toolwith the -change flag to change the install_name of a library after it's already been linked into the application, but that's obviously suboptimal. Fix it before linking.

您还可以使用install_name_tool-change 标志在库已链接到应用程序后更改其 install_name,但这显然不是最佳选择。在链接之前修复它。