我正在尝试使用f2py在我的系统中安装一些Python模块,这些模块与海洋模型相关(用Fortran90编写)。我在使用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 moduletestsub的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例程。这里的错误可能是什么?这是因为代码的结构吗?或者,我需要包含一些其他库吗?
发布于 2016-12-26 18:22:36
您正在编译共享(动态)库,并且应该使用NetCDF的共享库版本。
如果您自己安装了NetCDF (如/home/sonaljit中的路径所示),则应安装.so版本并与此版本链接。
https://stackoverflow.com/questions/41325631
复制相似问题