我有一个Fortran模块,我正在尝试用f2py编译它(如下所示)。当我删除模块声明并将子例程单独留在文件中时,一切工作正常。但是,如果如下所示声明了模块,我会得到以下结果:
> 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:\users\astay13\appdata\local\temp\tmpgh5ag8\Release\users\astay13\appdata\local\temp\tmpgh5ag8\src.win32-3.2\itsmodule.o:itsmodule.c:(.data+0xec): undefined reference to `itimes_'
collect2: ld returned 1 exit status在f2py中编译模块或子例程有什么不同?我是否在模块中遗漏了导致f2py出现问题的重要内容?请注意,当我单独使用gfortran时,该模块编译得很好。
软件: Windows 7;gcc,gfortran 4.6.1 (MinGW);python 3.2.2;f2py v2
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发布于 2011-12-20 16:36:36
您正在尝试将Fortran模块放在Python模块中。如果你想这样,名字必须不同,例如
f2py.py -c -m SOMEDIFFERENTNAME itimes-s2.f结果将被称为pythonmodule.fortranmodule.yourfunction()。
您也可以将其导入为
from pythonmodule import fortranmodule
fortranmodule.yourfunction()否则它会在我的机器上工作。
发布于 2011-12-20 01:41:42
要让f2py正常工作,您需要有一个签名文件来指导界面的创建,或者使用f2py注释来修改源代码以帮助创建界面。有关详细信息,请参阅http://cens.ioc.ee/projects/f2py2e/usersguide/#signature-file。
从该站点:
C FILE: FIB3.F
SUBROUTINE FIB(A,N)
C
C CALCULATE FIRST N FIBONACCI NUMBERS
C
INTEGER N
REAL*8 A(N)
Cf2py intent(in) n
Cf2py intent(out) a
Cf2py depend(n) a
DO I=1,N
IF (I.EQ.1) THEN
A(I) = 0.0D0
ELSEIF (I.EQ.2) THEN
A(I) = 1.0D0
ELSE
A(I) = A(I-1) + A(I-2)
ENDIF
ENDDO
END
C END FILE FIB3.F构建扩展模块现在可以在一个命令中执行:
f2py -c -m fib3 fib3.f发布于 2020-09-17 02:29:58
要用f2py包装F90模块,您确实需要指定哪些功能可以从模块外部访问。为此,您可以使用private和public fortran90关键字。您还应该将扩展名从.f更改为.f90或.F90。我在演示public和private关键字用法的代码中做了一些更改。
首先,创建保存模块对象的fortran接口的签名文件:
f2py -h interface.pyf -m module_name module_its.f90 --overwrite-signatureTHen编译模块:
f2py interface.pyf -c module_its.f90 您的代码被修改并命名为module_its.f90:
module its
! use other modules
implicit none
private ! everything in this module is private by standard
! (it can be used only inside this F90 module)
public :: itimes ! Make you subroutine public
contains
subroutine itimes(infile,outfile)
! use other modules here to
!implicit none holds for everthing in this module
! 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 itshttps://stackoverflow.com/questions/8564771
复制相似问题