使用 f2py 编译 Fortran 模块

时间:2023-04-15
本文介绍了使用 f2py 编译 Fortran 模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个 Fortran 模块,我正在尝试使用 f2py(如下所列)进行编译.当我删除模块声明并将子例程单独留在文件中时,一切正常.但是,如果模块声明如下所示,我会得到以下结果:

I have a Fortran module which I am trying to compile with f2py (listed below). When I remove the module declaration and leave the subroutine in the file by itself, everything works fine. However, if the module is declared as shown below, I get the following results:

> f2py.py -c -m its --compiler=mingw itimes-s2.f
...
Reading fortran codes...
    Reading file 'itimes-s2.f' (format:fix,strict)
crackline: groupcounter=1 groupname={0: '', 1: 'module', 2: 'interface', 3: 'subroutine'}
crackline: Mismatch of blocks encountered. Trying to fix it by assuming "end" statement.
...
c:usersastay13appdatalocal	emp	mpgh5ag8Releaseusersastay13appdatalocal	emp	mpgh5ag8src.win32-3.2itsmodule.o:itsmodule.c:(.data+0xec): undefined reference to `itimes_'
collect2: ld returned 1 exit status

在 f2py 中编译模块或子程序有什么不同?我是否在模块中遗漏了导致 f2py 出现问题的重要内容?请注意,当我单独使用 gfortran 时,模块编译得很好.

What is different about compiling a module or subroutine in f2py? Have I left something important out in the module that causes f2py to have trouble? Note that the module compiles fine when I use gfortran alone.

软件:Windows 7;gcc,gfortran 4.6.1(MinGW);蟒蛇3.2.2;f2py v2

Software: Windows 7; gcc, gfortran 4.6.1 (MinGW); python 3.2.2; f2py v2

times-s2.f:

itimes-s2.f:

  module its

  contains

  subroutine itimes(infile,outfile)

    implicit none

    ! Constants
    integer, parameter :: dp = selected_real_kind(15)

    ! Subroutine Inputs
    character(*), intent(in) :: infile
    character(*), intent(in) :: outfile

    ! Internal variables
    real(dp) :: num
    integer :: inu
    integer :: outu
    integer :: ios

    inu = 11
    outu = 22

    open(inu,file=infile,action='read')
    open(outu,file=outfile,action='write',access='append')

    do
      read(inu,*,IOSTAT=ios) num
      if (ios < 0) exit

      write(outu,*) num**2
    end do

  end subroutine itimes

  end module its

推荐答案

您试图在 Python 模块中包含 Fortran 模块.如果需要,名称必须不同,例如

You are trying to have a Fortran module in a Python module. If you want that, the names must be different, e.g.

 f2py.py -c -m SOMEDIFFERENTNAME itimes-s2.f

结果将被称为 pythonmodule.fortranmodule.yourfunction().

你也可以导入为

from pythonmodule import fortranmodule
fortranmodule.yourfunction()

否则它在我的机器上工作.

Otherwise it worked on my machine.

这篇关于使用 f2py 编译 Fortran 模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何轻松地将 FORTRAN 代码转换为 Python 代码(真实代码,而不是包装器) 下一篇:读取fortran未格式化文件时记录标记不一致

相关文章