简体   繁体   English

DYLD_LIBRARY_PATH / LD_LIBRARY_PATH的替代品

[英]alternatives to DYLD_LIBRARY_PATH/LD_LIBRARY_PATH

I'm developing python C++ extensions for use in both OSX and linux. 我正在开发可在OSX和Linux中使用的python C ++扩展。 Currently, I can run my code with a wrapper script wrapper.sh : 目前,我可以使用包装脚本wrapper.sh运行代码:

#!/bin/bash                                                                                                                              
trunk=`dirname $0`                                                                                                                       
trunk=`cd $trunk; pwd`                                                                                                                   
export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:$trunk/lib                                                                                   
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$trunk/lib/:$trunk/src/hdf5/lib/:$trunk/src/python/lib                                           
$trunk/src/python/bin/python "$@" 

which is able to set up my run like this: wrapper.sh app.py 能够像这样设置我的跑步: wrapper.sh app.py

What I would like to do is to eliminate the need for wrapper.sh , so I need alternatives for DYLD_LIBRARY_PATH and LD_LIBRARY_PATH. 我想做的是消除对wrapper.sh的需要,因此我需要DYLD_LIBRARY_PATH和LD_LIBRARY_PATH的替代方法。 I can not put my libraries in some standard location like /usr/local/lib because on my machine, I maintain several independent instances of my libraries. 我不能将库放在/usr/local/lib等标准位置,因为在我的机器上,我维护着几个独立的库实例。 That is, my libraries need to be kept somewhere relative to my installation path. 也就是说,我的库需要保留在相对于我的安装路径的某个位置。 I can't put these environment variables in my login script for the same reason. 由于相同的原因,我无法将这些环境变量放入我的登录脚本中。 Currently, I need to call one of my wrapper.sh scripts to use the associated libraries. 当前,我需要调用我的wrapper.sh脚本之一来使用关联的库。 My goal is to be able to run merely app.py , which if it lives in my installation path, should be able to find its associated python and libraries. 我的目标是仅运行app.py ,如果它位于我的安装路径中,则应该能够找到其关联的python和库。 The purpose is to simplify execution for users, and to simplify usage of external tools like nosetests. 目的是简化用户执行,并简化外部测试(例如鼻子测试)的使用。

One alternative seems to be using rpath when I build my version of python: 当我构建python版本时,一种替代方法似乎是使用rpath:

./configure --enable-shared --prefix=$(CURDIR)/$(PYTHON_DIR) LDFLAGS="-Wl,-rpath,$(CURDIR)/lib/ -Wl,-rpath,$(CURDIR)/src/hdf5/lib -Wl,-rpath,$(CURDIR)/src/python/lib"

This trick seems to work fine on linux, even though one of my libraries ended up needing to be copied directly into trunk/src/python/lib/python2.6/lib-dynload for some reason unclear to me. 即使我的一个库最终由于某种我不清楚的原因最终需要直接复制到trunk/src/python/lib/python2.6/lib-dynload ,该技巧在Linux上似乎也能正常工作。 However, this trick is not working on OSX; 但是,此技巧在OSX上无效。 it looks like I need to run install_name_tool on all my dylibs libraries. 看来我需要在所有dylibs库上运行install_name_tool

The other alternative I came up with was to do something like this: 我想到的另一个选择是做这样的事情:

ln -s wrapper.sh python

so that my scripts could all use #! ../python 这样我的脚本都可以使用#! ../python #! ../python , but I'm getting Unmatched ". errors. Same thing if I use #! ../wrapper.sh . I'm not really an expert in bash... #! ../python ,但出现Unmatched ".错误。如果我使用#! ../wrapper.sh 。我并不是bash的真正专家...

However, these all seem so unnecessarily complicated, and surely this is something that other people have solved?? 但是,这些似乎都变得不必要地复杂,这肯定是其他人已经解决的事情了吗? Thanks for any advice! 感谢您的任何建议!

For python extensions, consider using PYTHONPATH: the Python interpreter will search the PYTHONPATH for .py/.pyc/.pyo/.so modules, as well as packages. 对于python扩展,请考虑使用PYTHONPATH:Python解释器将在PYTHONPATH中搜索.py / .pyc / .pyo / .so模块以及包。 See docs for Python 2.x as well as docs for Python 3.x ; 请参阅适用于Python 2.x的 文档以及适用于Python 3.x的文档 specifically the section named "The Module Search Path" on both pages. 特别是在两个页面上名为“模块搜索路径”的部分。 This also references information that seems to indicate that it is possible to update the module search path at runtime, which, if true, means that you could add all that logic to your program and it can hunt for its libraries on its own (say if it installs a copy in /usr/libexec/pkgname/... somewhere or something). 这也引用了似乎表明可以在运行时更新模块搜索路径的信息,如果为true,则意味着您可以将所有逻辑添加到程序中,并且可以自行寻找其库(例如它会在/ usr / libexec / pkgname / ...某处安装副本。

For all but the most complex of cases, though, setting PYTHONPATH and using a shell-script or native-compiled binary wrapper to start the core program is an okay approach, and one that is also used in other language environments including Mono and Java. 但是,对于除最复杂的情​​况以外的所有情况,设置PYTHONPATH并使用shell脚本或本机编译的二进制包装器启动核心程序都是可以的方法,并且该方法还用于其他语言环境(包括Mono和Java)中。

Not sure if this would be an acceptable (partial) solution in your circumstances, but another way to get libraries noticed by ld on linux is to add the path to the libraries to /etc/ld.so.conf and then run ldconfig 不知道这在您的情况下是否可以接受(部分)解决方案,但是让ld在Linux上引起注意的库的另一种方法是将库的路径添加到/etc/ld.so.conf ,然后运行ldconfig

For the Mac I don't remember the details, but I think Apple provide some resources for distributing apps packaged as a .app which includes some default locations (relative to the root of the .app) for libraries, or "frameworks" as they call them. 对于Mac,我不记得详细信息,但我认为Apple提供了一些资源来分发打包为.app的应用程序,其中包括库或“框架”的某些默认位置(相对于.app的根目录)给他们打电话。 Would require some googling from there - sorry can't help further on that but hope you get some progress :-) 需要从那里进行谷歌搜索-很抱歉,您对此无济于事,但希望您能有所进步:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM