使用Python Egg/Wh打包共享对象(库)

2024-05-18 09:40:43 发布

您现在位置:Python中文网/ 问答频道 /正文

到目前为止,我已经做到了:

  1. 创建了MANIFEST.in,使用:include path/to/libfoo.so
  2. 创建了setup.py,在调用setupt.py install之后,将libfoo.so放入/usr/local/lib/python/site-packages/foo.egg/path/to/libfoo.so。在

当然,这无助于Python在运行时需要时找到libfoo。我需要做些什么才能让Python真正找到这个库?在

这个库没有Python绑定,它只是一个共享库,其中包含一些本机代码。它从位于/usr/local/lib/python/site-packages/foo.egg/path/wrapped.cpython-36m-x86_64-linux-gnu.so中的另一个共享库调用。在


Tags: topathinpysofooincludeegg
1条回答
网友
1楼 · 发布于 2024-05-18 09:40:43

如果要硬编码共享库的位置,可以使用rpath选项。为此你会做一些类似。。在

python setup.py build_ext  rpath=/usr/local/lib/python/site-packages/foo.egg/path/to

在哪里设置.py上面是用于构建wrapped.cpython-36m-x86_64-linux-gnu.so的脚本,rpathlibfoo.so的路径。当然,您应该能够将其直接放入构建脚本中,具体取决于该过程的外观。在

-rpath=dir

Add a directory to the runtime library search path. This is used when linking an ELF executable with shared objects. All -rpath arguments are concatenated and passed to the runtime linker, which uses them to locate shared objects at runtime. The -rpath option is also used when locating shared objects which are needed by shared objects explicitly included in the link

如果不能更新wrapped.cpython-36m-x86_64-linux-gnu.so的生成过程,我认为您唯一的选择是将libfoo.so放在加载库路径中的某个位置,或者在运行时手动添加该位置。在

为了回答你接下来的几个问题。。。在

系统加载库位置来自/etc/ld.so.conf,并引用ld.so.conf.d目录中的位置。ldconfig命令根据此数据重建共享库的缓存,因此如果您更改了某些内容,请务必调用此命令。在

在命令行或在.bashrc中,可以使用export LD_LIBRARY_PATH=....将其他目录添加到搜索路径中。在

可以手动加载共享对象。请参见https://docs.python.org/2/library/ctypes.html加载共享库。在

我自己没有尝试过,但是我读过,如果你在python代码中手动加载一个下级共享库,然后导入更高级别的库,那么链接器就不必再去寻找较低的库了,因为它已经加载了。这看起来像。。。在

^{pr2}$

在StackOverflow上有很多关于如何做到这一点的例子,还有很多关于操作库搜索路径的附加信息/示例。在

相关问题 更多 >