繁体   English   中英

f2py无法与NetCDF库链接-

[英]f2py is not able to link with NetCDF library -

我正在尝试使用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例程。 这可能是什么错误? 是由于代码的结构吗? 或者,我需要包括其他图书馆吗?

您正在编译共享(动态)库,并且应该使用NetCDF的共享库版本。

如果您自己安装了NetCDF(如/home/sonaljit的路径所建议),则应安装.so版本并与此版本链接。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM