4

I need to wrap a simply fortran90 code with f2py. The fortran module "test.f90" is

module util

contains

FUNCTION gasdev(idum)
implicit none
INTEGER(kind=4), intent(inout) :: idum
REAL(kind=8) :: gasdev, ran2
print*,idum
gasdev = ran2(idum)
return
END FUNCTION

FUNCTION ran2(idum)
implicit none
INTEGER(kind=4), intent(inout) :: idum
REAL(kind=8) :: ran2
print*,idum
ran2=2.D0
return
END FUNCTION
end module util

and then I wrap it with

f2py  -m test -c test.f90

but when I import it in python

In [2]: import test

it prompted me with error saying

ImportError: ./test.so: undefined symbol: ran2_

Any ideas on how to fix it? Thanks.

nye17
  • 12,857
  • 11
  • 58
  • 68

1 Answers1

6

In function gasdev you declare ran2 as an external function. As you then don't link in any such function importing the module will fail.

Instead, remove the declaration of ran2 in gasdev, in which case the ran2 call uses the explicit interface to the ran2 function in the module, and everything works.

janneb
  • 36,249
  • 2
  • 81
  • 97
  • thanks. the depressing part is that the code works well in fortran compiler itself, and the error spit out by f2py is not quite illuminating to me... – nye17 Oct 18 '11 at 18:10
  • Sure, compiling will work fine. Linking might be another matter though, unless you have an external procedure ran2 somewhere in your codebase. – janneb Oct 18 '11 at 20:21
  • I used to think that I have to designate an external function using `external` keyword, never realized that it is already so when I declare it... – nye17 Oct 18 '11 at 21:09