5

I would like to make so file in order to use it in python. how can I make shared library from fortran source?

I have tested like below code.

gfortran -c mod.f90
#gfortran -c sub1.f90
gfortran -c func.f90
gfortran -shared -fPIC -o func.so func.f90 mod.o

but I couldn't import it in python. I used module file in fortran source code. and I imported fortran source code from python. I'm not sure if I do right.

[===>14:47:59]f1:python
Python 2.7.2+ (default, Oct  4 2011, 20:03:08) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import func
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dynamic module does not define init function (initfunc)




func.f90
-----------------------------------------
program func

use mod_test

switch1 = .true.
switch2 = .false.

x = 1.2
!call test(x, z)
print *, b, str, z, switch1, switch2

!print *, 'hello'
end program func
-----------------------------------------
mod.f90
-----------------------------------------
module mod_test
!implicit none
integer a
real x, y, z
real*8 :: b = 3.4
logical*2 switch1, switch2
character*5, parameter :: str = 'good'
end module mod_test
-----------------------------------------
sub1.f90
-----------------------------------------
subroutine test(input, output)
real, intent(in) :: input
real, intent(out) :: output

output = (input + input)
end subroutine
-----------------------------------------
wonjun
  • 311
  • 2
  • 10
  • I also tested : fwrapc func.f90 --build --name=func --fcompiler=gnu95 --f90exec=/usr/bin/gfortran-4.6 -L/usr/lib/gcc/i686-linux-gnu/4.6 -lgfortran but it cannot find the module. – wonjun Feb 15 '12 at 06:53

1 Answers1

11

You need some "glue" between Fortran and Python. Check out F2PY - Fortran to Python interface generator

EDIT. Example:

f2py -c -m func func.f90 mod.f90 sub1.f90
python
>>> import func
>>> dir(func)
['__doc__', '__file__', '__name__', '__version__', 'mod_test', 'test']

EDIT 2. If you want to execute the code in func.f90 from Python, I think you must change it from program to a subroutine.

Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
Janne Karila
  • 24,266
  • 6
  • 53
  • 94
  • f2py -c mod.f90; f2py -c -m func func.f90; I did like this but the module couldn't be found..how can I do? – wonjun Feb 15 '12 at 07:18
  • "the module couldn't be found" -- is that an error message from f2py? – Janne Karila Feb 15 '12 at 07:50
  • f2py -c -m func func.f90 mod.f90 sub1.f90 is to create executable file. but I want to make a shared library in order to import it from Python. and the error message was from Python : ImportError: ./func.so: undefined symbol: __mod_test_MOD_b – wonjun Feb 15 '12 at 09:33
  • @wonjun A shared library func.so is exactly what `f2py -c -m func` builds for you. Maybe some results from you previous attempts interfere. Copy the three f90 files to an empty subdirectory and try again there. – Janne Karila Feb 15 '12 at 09:38
  • I could make so file but the subroutine test in sub1.f90. cannot be called in Python. >>> import func >>> dir(func) ['__doc__', '__file__', '__name__', '__package__', '__version__', 'mod_test'] – wonjun Feb 15 '12 at 09:47
  • For me, dir(func) includes 'test'. – Janne Karila Feb 15 '12 at 09:58
  • I called it from fortran like this : call test(x, z) and in python, ImportError: ./func.so: undefined symbol: test_ – wonjun Feb 15 '12 at 09:59