2011-12-19 123 views
3

我有一個Fortran模塊,我試圖用f2py編譯(下面列出)。當我刪除模塊聲明並將它自己的子例程留在文件中時,一切正常。但是,如果模塊聲明如下,我得到以下結果:使用f2py編譯fortran模塊

> 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 

回答

8

你想有一個Python模塊在一個Fortran模塊。如果你想要,名稱必須不同,例如

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

結果將被稱爲pythonmodule.fortranmodule.yourfunction()

否則它在我的機器上工作。

+0

我試着運行'f2py -c --compiler = mingw32 -m itsm itimes-s2.f',但是錯誤信息仍然是一樣的。 – astay13 2011-12-20 15:19:20

+0

嘗試重命名文件以使其具有.f90後綴。似乎編譯器認爲它是一個固定格式的文件(至少在我的機器上)。我正在使用'f2py -c -m itsm itimes-s2.f90',它可以工作。我在2臺不同的linux電腦上測試過它。 – 2011-12-20 20:54:28

+0

謝謝弗拉基米爾!一旦我將其重命名爲'.f90'擴展名,即使Python和Fortran模塊具有相同的名稱,它也可以很好地工作。 – astay13 2011-12-20 21:50:57

2

對於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 
+0

對,但是我的問題是,當子程序自己在文件中列出時,f2py可以正常工作,但是當我將它包含在模塊中時會給出錯誤。從你的鏈接看來,我應該能夠使用我的源代碼(也許還有一些額外的指令)作爲我的簽名文件。我需要包含哪些其他指令,以便它接受模塊? – astay13 2011-12-19 18:49:35