如何在 Xcode 中设置 dyld_library_path

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

How to set dyld_library_path in Xcode

xcodemacosxcode4dylibdyld

提问by Navin

I am new to Xcode and Mac environment. I am using some dynamic and static libraries like boost, Clucene, etc. I have all the libraries under

我是 Xcode 和 Mac 环境的新手。我正在使用一些动态和静态库,如 boost、Clucene 等。我有所有的库

MyApp.app/Contents/Resources

MyApp.app/内容/资源

I want to set this path as the app's dyld_library_path. I tried editing XXX.plist file like

我想将此路径设置为应用程序的 dyld_library_path。我尝试编辑 XXX.plist 文件,例如

DYLD_LIBRARY_PATH /mypath/xxx

DYLD_LIBRARY_PATH /mypath/xxx

and setting the environment variable and argument in Xcode Nothing work.

并在 Xcode 中设置环境变量和参数没有任何作用。

but if I run a shell script like below without double clicking the app in my .dmg it works

但是如果我运行一个像下面这样的 shell 脚本而不双击我的 .dmg 中的应用程序,它就可以工作

#!/bin/bash
clear
cd /Volumes/xxx/myapp.app/Contents/MacOS
export DYLD_LIBRARY_PATH="/Volumes/xxx/myapp.app/Contents/Resources"
./myapp

I am sure this is not the proper way to do this. Is there proper way to set dyld_library_path every time I execute my app?

我确信这不是执行此操作的正确方法。每次执行我的应用程序时,是否有正确的方法来设置 dyld_library_path?

EDIT: It also works if u mannualy copy all ur library to clients /usr/lib path... i guess this is also not a proper way to do it.

编辑:如果您手动将您的所有库复制到客户端 /usr/lib 路径,它也可以工作...我想这也不是正确的方法。

回答by bdash

Setting DYLD_LIBRARY_PATHisn't the best way to solve this problem. It's working around the fact that you've misinformed dyldas to where to find your libraries.

设置DYLD_LIBRARY_PATH并不是解决这个问题的最好方法。它正在解决您错误地告知dyld在哪里可以找到您的库的事实。

If you run otool -L MyApp.app/Contents/MacOS/MyAppyou'll see the paths to the libraries that MyApp wants to load. If any library isn't found at the specified path then dyldwill look for the library in the locations specified by DYLD_FALLBACK_LIBRARY_PATH. Setting DYLD_LIBRARY_PATHcauses dyldto look for the library in the given locations ahead of the path that the otoolcommand above returned.

如果您运行,otool -L MyApp.app/Contents/MacOS/MyApp您将看到 MyApp 要加载的库的路径。如果在指定路径中未找到任何库,则将在指定dyld的位置查找库DYLD_FALLBACK_LIBRARY_PATH。设置DYLD_LIBRARY_PATH导致dyld在上述otool命令返回的路径之前的给定位置中查找库。

The best way to solve this problem is to have your application specify the correct location of the libraries to start with so that setting DYLD_LIBRARY_PATHis not necessary. To do this you need to do the following:

解决这个问题的最好方法是让你的应用程序指定库的正确位置开始,这样设置DYLD_LIBRARY_PATH就没有必要了。为此,您需要执行以下操作:

  1. Set the library identifier of each of the libraries that you're bundling inside your application to an @rpath-relative value. You can do this using install_name_tool -id @rpath/libFoo.dylib libFoo.dylib.
  2. Add a Copy Files build phase to copy the libraries in to your application wrapper. MyApp.app/Contents/Frameworksis a typical location. MyApp.app/Contents/Resourcesshould be avoided since binaries aren't resources in the usual sense of the term.
  3. Specify a run path search path when linking your application. This gives the linker a list of paths to use to resolve any @rpathvariables that it encounters in any load commands. If you're copying the libraries to MyApp.app/Contents/Frameworksyou'll want to specify a run path search path of @loader_path/../Frameworks. You can do this via the LD_RUNPATH_SEARCH_PATHS(Runpath Search Paths) configuration setting in Xcode on your application target.
  1. 将您在应用程序中捆绑的每个库的库标识符设置为@rpath-relative 值。您可以使用install_name_tool -id @rpath/libFoo.dylib libFoo.dylib.
  2. 添加复制文件构建阶段以将库复制到应用程序包装器中。MyApp.app/Contents/Frameworks是一个典型的位置。MyApp.app/Contents/Resources应该避免,因为二进制文件不是该术语通常意义上的资源。
  3. 链接应用程序时指定运行路径搜索路径。这为链接器提供了一个路径列表,用于解析@rpath它在任何加载命令中遇到的任何变量。如果您要将库复制到MyApp.app/Contents/Frameworks您需要指定的运行路径搜索路径@loader_path/../Frameworks. 您可以通过LD_RUNPATH_SEARCH_PATHS应用程序目标上 Xcode 中的(Runpath Search Paths) 配置设置来执行此操作。

After doing all this you should be able to re-run the otoolcommand mentioned above and see that the paths to your library are using @rpath-relative paths. You should then be able to run otool -lV MyApp.app/Contents/MacOS/MyAppand see an LC_RPATHload command specified with a value of @loader_path/../Frameworks. Finally, you should be able to run your application and see that it finds the libraries within its Frameworks directory without having DYLD_LIBRARY_PATHset!

完成所有这些之后,您应该能够重新运行otool上面提到的命令,并看到您的库的路径正在使用@rpath-relative 路径。然后,您应该能够运行otool -lV MyApp.app/Contents/MacOS/MyApp并查看LC_RPATH指定值为 的加载命令@loader_path/../Frameworks。最后,您应该能够运行您的应用程序并看到它在其 Frameworks 目录中找到了没有DYLD_LIBRARY_PATH设置的库!