Calling FORTRAN Shared Libraries from Gambas
Posted
#1
(In Topic #246)
Regular

Uhh, because we can?
No, seriously, there are a lot of old FORTRAN libraries and old FORTRAN codgers. Some of those routines are quite clever.
So, just because we can. A few things to watch out for:
* FORTRAN mangles the routine names. I had to do a "nm -D" to figure that out.
* FORTRAN passes by reference
* I didn't test much except passing values to the routines for each of the FORTRAN types.
So without further ado, here is the source code for the FORTRAN library, Test1.for. Compiling instructions at the bottom.
Code
SUBROUTINE Test1()
WRITE( *, * ) "Hello from FORTRAN land!"
END
SUBROUTINE Test2( N )
INTEGER N
WRITE( *, * ) N
N = N + 1
END
SUBROUTINE Test3( R )
REAL R
WRITE( *, * ) R
R = R + 1.0
END
SUBROUTINE Test4( D )
DOUBLE PRECISION D
WRITE( *, * ) D
D = D + 1.0
END
SUBROUTINE Test5( L )
LOGICAL L
WRITE( *, * ) L
L = (4.GT.5)
END
SUBROUTINE Test6( C )
CHARACTER*20 C
WRITE( *, * ) C
C = "98765432109876543210"
END
SUBROUTINE Test7( E )
COMPLEX E
WRITE( *, * ) E
E = E + 1.0
END
C gfortran -shared -fPIC -o Test1.so Test1.for
C nm -D Test1.so
Code (gambas)
Here is the output:
Code
Hello world
Hello from FORTRAN land!
3122
6.28318548
2.7182818284590451
T
12345678901234567890
( 3.00000000 , 4.00000000 )
I,m back
3123
7.2831855
3.71828182845905
False
98765432109876543210
4 4
Now, if anybody has a better way to do the character and complex types without resorting to a Alloc/Free pair, I'd appreciate knowing about it.
Ced
.... and carry a big stick!
Posted
Regular

Anyway, I have updated the code to reflect this, made the subroutines modify the calling arguments, and have the Gambas program print the results.
The updates have been applied to the previous post.
Use at your own risk, yada, yada.
I'm still interested in improving the Character and Complex cases.
.... and carry a big stick!
1 guest and 0 members have just viewed this.



