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
-----------------------------------------
gfortran -shared -fPIC -o cfdrc_user_access.so cfdrc_user_access.f90 gfortran -shared -fPIC -o cfdrc_user.so my_library.f90 First, compile the program to an object file (.o), using the -c flag as shown on the first line. Then use gfortran to link the object file with the shared libraries created in the previous step:
I’m using GNU Fortran ( gfortran) to build several shared libraries, and then dynamically linking to them from a Fortran program. The process is a little different than what I’m used to for C libraries, so I thought I’d explain it.
But this is not the common way to work with shared libraries on windows because just a few compilers support this - the Microsoft Visual Studio Compiler is not among them. On Windows, a DLL usually has a matching so called “import library”. If an executable wants to use functions of a DLL it does not link the DLL directly but it’s import library.
Now let’s compile our library with GCC: The command above compiles shared.cpp to a shared library ( -shared) with position independent code ( -fPIC ). The compiler output ( -o) is written to libshared.so . Let’s take a quick look external symbols of our library. We can use “nm” for this purpose:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With