如何在Xcode中设置dyld_library_path [英] How to set dyld_library_path in Xcode

查看:1249
本文介绍了如何在Xcode中设置dyld_library_path的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

MyApp.app/内容/资源

MyApp.app/Contents/Resources

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

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

DYLD_LIBRARY_PATH/mypath/xxx

DYLD_LIBRARY_PATH /mypath/xxx

并在Xcode中设置环境变量和参数无效.

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

但是如果我运行如下所示的shell脚本而不双击.dmg中的应用程序,它将起作用

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

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

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

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?

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

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.

推荐答案

设置DYLD_LIBRARY_PATH并不是解决此问题的最佳方法.这是因为您误解了dyld有关在何处找到库的事实.

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

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

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

解决此问题的最佳方法是让您的应用程序指定开始的库的正确位置,因此无需设置DYLD_LIBRARY_PATH.为此,您需要执行以下操作:

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_PATH is not necessary. To do this you need to do the following:

  1. 将要绑定到应用程序中的每个库的库标识符设置为相对值@rpath.您可以使用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.您可以通过应用程序目标上Xcode中的LD_RUNPATH_SEARCH_PATHS(运行路径搜索路径)配置设置来完成此操作.
  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/Frameworks is a typical location. MyApp.app/Contents/Resources should 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 @rpath variables that it encounters in any load commands. If you're copying the libraries to MyApp.app/Contents/Frameworks you'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.

完成所有这些操作后,您应该能够重新运行上面提到的otool命令,并看到库的路径使用的是@rpath相对路径.然后,您应该能够运行otool -lV MyApp.app/Contents/MacOS/MyApp并看到指定为@loader_path/../Frameworks值的LC_RPATH加载命令.最后,您应该能够运行您的应用程序,并看到它无需设置DYLD_LIBRARY_PATH就可以在其Frameworks目录中找到这些库!

After doing all this you should be able to re-run the otool command 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/MyApp and see an LC_RPATH load 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_PATH set!

这篇关于如何在Xcode中设置dyld_library_path的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆