Compile fortran module with f2py

I have a Fortran module that I am trying to compile with f2py (see below). When I delete 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:\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 

What is the difference between compiling a module or subroutine in f2py? I left something important in the module, why did f2py have problems? Please note that the module compiles fine when I use only gfortran.

Software: 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 
+4
source share
2 answers

You are trying to create a Fortran module in a Python module. If you want this, the names should be different, for example.

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

The result will be called pythonmodule.fortranmodule.yourfunction() .

Otherwise, he worked on my machine.

+8
source

For f2py to work, you need to have a signature file to direct the creation of the interface or change the source code with f2py comments to help with the interface. See http://cens.ioc.ee/projects/f2py2e/usersguide/#signature-file for details.

From this site:

 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 

Creating an extension module can now be done with a single command:

 f2py -c -m fib3 fib3.f 
+2
source

Source: https://habr.com/ru/post/1387069/


All Articles