共享对象文件应该放在哪里?
我正在尝试使用 pybindgen 为 Python 创建 C/C++ 绑定。我已按照“构建它(GCC 指令)”中概述的步骤为示例文件创建绑定:
http://packages.python.org/PyBindGen/tutorial.html#a-simple-example
运行 make
会生成一个 .so 文件。如果我了解 .so 文件的工作原理,我应该能够将共享对象中的类导入到 Python 中。但是,我不确定该文件放在哪里以及如何让Python知道它在哪里。另外,原始的c/c++源文件是否需要附带.so文件?
到目前为止,我已尝试将文件放入 /usr/local/lib 并将该路径添加到 DYLD_LIBRARY_PATH 到 .bash_profile。当我尝试从 Python 解释器中导入模块时,会抛出错误,指出找不到该模块。
所以,我的问题是:需要对生成的 .so 文件做什么才能让 Python 程序使用它?
I am venturing into the land of creating C/C++ bindings for Python using pybindgen. I've followed the steps outlined under "Building it ( GCC instructions )" to create bindings for the sample files:
http://packages.python.org/PyBindGen/tutorial.html#a-simple-example
Running make
produces a .so file. If I understand how .so files work, I should be able to import
the classes in the shared object into Python. However, I'm not sure where to place the file and how to let Python know where it is. Additionally, do the original c/c++ source files need to accompany the .so file?
So far I've tried placing the file in /usr/local/lib and adding that path to DYLD_LIBRARY_PATH to the .bash_profile. When I try to import the module from within the Python interpeter an error is thrown stating that the module can not be found.
So, my question is: What needs to be done with the generated .so file in order for it to be used by a Python program?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Python 在搜索 python 模块的同一目录中查找
.so
模块。所以你必须像普通的 python 模块一样安装它,或者默认在 python 的 sys.path 上的某个地方(/usr/share/python/site-lib
或者其他地方)就像那样 - 它依赖于发行版)或将目录添加到 PYTHONPATH 环境变量中。python 使用 dlopen 加载模块,而不是动态链接器,因此 LD_LIBRARY_PATH(注意,没有 DY)不会帮助您。
Python looks for
.so
modules in the same directories where it searches python ones. So you have to install it as you would normal python module either somewhere that is on python'ssys.path
by default (/usr/share/python/site-lib
or something like that—it'd distribution-dependent) or add the directory toPYTHONPATH
environment variable.It's python that's loading the module using dlopen, not the dynamic linker, so
LD_LIBRARY_PATH
(note, there is noDY
) won't help you.与所有其他 Python 模块相同。它必须位于 sys.path 中指定的位置之一内。
Same as all other Python modules. It must be within one of the locations given in
sys.path
.