在用f2py包装fortran代码时,是否可以使用静态链接?

2024-06-09 08:48:09 发布

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

正如我在前面的question中所讨论的,我正在尝试在远程服务器上运行一个使用f2py包装的Fortran代码的python脚本。如果我在自己的pc上本地编译f2py模块,然后在服务器上运行脚本,我会得到以下错误:

ImportError: liblapack.so.3: cannot open shared object file: No such file or directory

这是合乎逻辑的,因为lapack和blas都没有安装在远程服务器上,遗憾的是,我没有安装它们的root权限。我想知道我是否可以通过使用静态链接来解决这个问题。关于GNU wiki,有人指出

One way to avoid this (more ideas can be found on the binaries page) is to use the so-called "static linking", available with option -static. Gfortran then puts the library code inside the program created, thus enabling it to run without the library present (like, on a computer where gfortran is not installed).

这听起来正是我需要的。如果所需的库文件包含在f2py模块中,那么我不需要远程服务器上的lapack和blas模块,我可以按原样运行它。然而,我不确定这种静态链接是否真的适用于f2py。目前我在跑步

python -m numpy.f2py -llapack -lblas -c --fcompiler=gnu95 --compiler=unix signature_file.pyf 
object_files.o

object_files.o指编译成目标文件的所有相互依赖的Fortran文件,即i ran

gfortran -c -llapack -lblas object_file.F90 

在所有相关文件上。现在我想用静态链接来实现这一点,但是如何实现呢?我在编译对象文件时尝试过添加-static,但这并没有改变任何东西。我还尝试将-static添加到f2py调用中,但这只是给出了一个未知的选项错误


Tags: 模块文件theto服务器脚本远程object