【发布时间】:2016-12-26 01:08:17
【问题描述】:
我正在尝试使用 f2py 在我的系统中安装一些 Python 模块,这些模块与海洋模型(在 Fortran 90 中)有关。我在使用 f2py 时遇到了一些问题。具体来说,即使我安装了所需的库和包含文件,f2py 也无法与 NetCDF 库链接。我在 64 位机器上的 Ubuntu16.04 上使用 Python 2.7 和 Anaconda 2。我正在使用 gfortran。
为了测试它是否正常工作,我编写了一个小代码 - 一个包含一个小子例程的 f90 模块。该子例程执行一项基本数学任务,并调用一个打印已安装 NetCDF 版本的 NetCDF 例程。模块(testsub.f90)如下:
module testsub
implicit none
contains
subroutine f_sum(a, b, s)
!#include 'netcdf.inc'
use netcdf
real(8) :: a, b, s
s = a+b;
!Calls a function that prints the netcdf version
write(*,*) trim(nf90_inq_libvers())
end subroutine f_sum
end module
testsub 的 makefile 是:
#Fortran compiler
FC=gfortran
NCLIB = -L/home/sonaljit/anaconda2/lib -lnetcdf -lnetcdff -L/usr/lib/python2.7/config-x86_64-linux-gnu -lpython2.7
NCINC = -I/home/sonaljit/anaconda2/include
#f2py and flags
F2PY = /home/sonaljit/anaconda2/bin/f2py
PYFLAGS = "-fPIC -g -O2 -fdefault-real-8"
pytest : testsub.f90
$(F2PY) --fcompiler=$(FC) --f90flags=$(PYFLAGS) -c $(NCINC) -m testpymod testsub.f90 $(NCLIB)
clean :
rm testpymod.so
我有 NetCDF 库并包含安装在给定路径中的文件。当我使用make pytest 运行makefile 时,出现以下错误:
/usr/bin/ld: /home/sonaljit/anaconda2/lib/libnetcdf.a(netcdf.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
/home/sonaljit/anaconda2/lib/libnetcdf.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
但是,当我注释掉模块中的 NetCDF 行时,我没有看到此错误。 f2py 似乎无法链接到 NetCDF 例程。这里有什么错误?是因为代码的结构吗?或者,我是否需要包含其他一些库?
【问题讨论】:
-
而且顺便说一句,Fortran 90 已经很老了,大多数程序都是 Fortran 95 甚至 2003,甚至可能是你的海洋模型。
标签: python fortran gfortran netcdf f2py