gpt4 book ai didi

python - 如何告诉 f2py 模块在当前目录中查找共享对象依赖项

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:03:14 25 4
gpt4 key购买 nike

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 环境变量?

来源:

库.f:

  subroutine helloworld()
print*, "hello world"
return
end subroutine

编.f:

  program helloworldprog
call helloworld()
end program helloworldprog

f2pyprog.f:

  subroutine pyhelloworld()
call helloworld()
return
end subroutine

测试.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()

最佳答案

  1. 设置环境变量export LDFLAGS=-Wl,-rpath=.
  2. 设置环境变量export NPY_DISTUTILS_APPEND_FLAGS=1
  3. numpy 升级到 1.16.0 或更高版本

虽然您不能在 f2py 中从命令行传递链接器标志,但它会读取 LDFLAGS 环境变量。但是,默认 behavior for numpy is to overwrite the flags用于编译而不是附加它们,如果 LDFLAGS 中不存在所需的标志,这将导致编译失败。 Support was added in numpy version 1.16.0通过设置环境变量 NPY_DISTUTILS_APPEND_FLAGS=1

选择性地附加这些链接器标志
>>> unset LD_LIBRARY_FLAGS   # just in case was set before
>>> export LDFLAGS=-Wl,-rpath=.
>>> export NPY_DISTUTILS_APPEND_FLAGS=1
>>> python3 -m numpy.f2py -c progpy.f lib.so -m prog
>>> python3 test.py
hello world

关于python - 如何告诉 f2py 模块在当前目录中查找共享对象依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54201961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com