【问题标题】:f2py is not able to link with NetCDF library -f2py 无法与 NetCDF 库链接 -
【发布时间】: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 例程。这里有什么错误?是因为代码的结构吗?或者,我是否需要包含其他一些库?

【问题讨论】:

  • 更喜欢通用标签而不是版本标签来吸引观众。使用fortranpython。否则人们不会看到你的问题。
  • 而且顺便说一句,Fortran 90 已经很老了,大多数程序都是 Fortran 95 甚至 2003,甚至可能是你的海洋模型。

标签: python fortran gfortran netcdf f2py


【解决方案1】:

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

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

【讨论】:

  • 感谢您的回复。我为 libnetcdff 安装了 .so 共享版本。我将该库包含在 makefile 中,并且它起作用了。非常感谢您的回答,弗拉基米尔。
  • 是的,答案确实有帮助。我正在使用一个静态的 .a 库,它没有完成这项工作。与 .so 共享库链接解决了这个问题。
猜你喜欢
  • 2013-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-31
  • 2012-06-07
  • 1970-01-01
相关资源
最近更新 更多