【发布时间】:2019-01-15 15:29:41
【问题描述】:
system: lubuntu 18.04, running in VirtualBox
假设我有以下源目录(底部的代码):
/f2pyproject/
- lib.f
- prog.f
- f2pyprog.f
- test.py
prog.f 是一个简单的 fortran 可执行文件,它将调用从lib.f 编译的共享对象中的子例程。
要实现这一点:
>>> gfortran -shared lib.f -o lib.so
>>> gfortran prog.f lib.so -o prog.exe -Wl,-rpath=.
>>> ./prog.exe
hello world
-Wl,-rpath=. 选项告诉 prog.exe 在当前目录中查找其链接的共享对象,这样我就不用担心$LD_LIBRARY_PATH
现在我想在 python 中调用这个相同的链接子例程,所以我用 f2py 调用编译 f2pyprog.f:
>>> python3 -m numpy.f2py -c f2pyprog.f lib.so -m prog
现在在这种情况下,prog.cpython-blah-blah.so 是一个共享对象,而不是一个可执行文件,所以我不知道的是,如何调用此工作流而不必担心 LD_LIBRARY_PATH 但将共享对象保存在与f2py 编译库。
调用 test.py 失败:
>>> python3 test.py (fails with ImportError, cannot open shared object file)
先设置 LD_LIBRARY_PATH 成功:
>>> export LD_LIBRARY_PATH=`pwd`
>>> python3 test.py
hello world
主要问题:
是否可以使用类似 -rpath 链接器选项的方式在当前目录中链接共享对象来构建这个(或任何)f2py 扩展,而不必担心$LD_LIBRARY_PATH 环境变量?
来源:
lib.f:
subroutine helloworld()
print*, "hello world"
return
end subroutine
prog.f:
program helloworldprog
call helloworld()
end program helloworldprog
f2pyprog.f:
subroutine pyhelloworld()
call helloworld()
return
end subroutine
test.py
import os
from os import path
# has no effect, presumably because this needs to be set before python starts
os.environ['LD_LIBRARY_PATH'] = path.abspath(path.dirname(__file__))
import prog
prog.pyhelloworld()
【问题讨论】:
标签: python linux shared-libraries f2py