I have these files:
$ ls
test.f90 module_file.f90
and module_file.f90 defines a module:
! module_file.f90
module test_mod
implicit none
....
contains
subroutine sub_in_module()
....
end subroutine sub_in_module
end module test_mod
Then module_file.f90 is compiled by gfortran -c module_file.f90, generates module_file.o and test_mod.mod.
The subroutine in test.f90 will call the sub_in_module:
! test.f90
subroutine sub()
use test_mod
implicit none
write(*,*) "calling sub_in_module in sub..."
call sub_in_module()
return
end subroutine sub
Then I want to call sub in python, so I use f2py:
$ f2py -c test.f90 -m f90test
and successfully generate f90test.cpython-36m-x86_64-linux-gnu.so.
But it can not work well,
$ python
>>> import f90test
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: /mypath/f90test.cpython-36m-x86_64-linux-gnu.so: undefined symbol: __test_mod_MOD_sub_in_module
>>>
I guess that it doesn't link to the module_file.o, but I have no idea how to solve this problem.
Any suggestion is helpful, thanks!
---------update---------
I successfully compile these files by following this.
Here is the procedure:
$ gfortran -c module_file.f90 -fPIC
$ f2py -c -I. module_file.o -m f90test test.f90
$ python
>>> import f90test # it works !
>>>