Calling FORTRAN Shared Libraries from Gambas

Post

Posted
Rating:
#1 (In Topic #246)
Avatar
Regular
Cedron is in the usergroup ‘Regular’
Why?

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
Here is the Gambas calling routine:

Code (gambas)

  1. ' Gambas module file
  2.  
  3. Extern test1_() In "~/Gambas/Fortran/Test1"
  4. Extern test2_(argIntegerPtr As Pointer) In "~/Gambas/Fortran/Test1"
  5. Extern test3_(argSinglePtr As Pointer) In "~/Gambas/Fortran/Test1"
  6. Extern test4_(argFloatPtr As Pointer) In "~/Gambas/Fortran/Test1"
  7. Extern test5_(argIntegerPtr As Pointer) In "~/Gambas/Fortran/Test1"
  8. Extern test6_(argAllocPtr As Pointer) In "~/Gambas/Fortran/Test1"
  9. Extern test7_(argAllocPtr As Pointer) In "~/Gambas/Fortran/Test1"
  10.  
  11. Public Sub Main()
  12.  
  13.   Print "Hello world"
  14.  
  15.   Dim I As Integer  ' FORTRAN INTEGER
  16.   Dim R As Single   ' FORTRAN REAL
  17.   Dim D As Float    ' FORTRAN DOUBLE PRECISION
  18.   Dim L As Integer  ' FORTRAN LOGICAL
  19.   Dim C As Pointer  ' FORTRAN CHARACTER
  20.   Dim E As Pointer  ' FORTRAN COMPLEX
  21.  
  22.   Dim O As Stream  
  23.  
  24.   I = 3122
  25.   R = Pi(2)  
  26.   D = Exp(1)
  27.   L = CInt(True) And 1
  28.   C = Alloc("12345678901234567890")
  29.   E = Alloc(8)
  30.  
  31.   O = Open Memory E For Write
  32.   Print #O, MkSingle(3);
  33.   Print #O, MkSingle(4);
  34.   Close #O
  35.  
  36.   test1_()
  37.   test2_(VarPtr(I))
  38.   test3_(VarPtr(R))
  39.   test4_(VarPtr(D))
  40.   test5_(VarPtr(L))
  41.   test6_(C)
  42.   test7_(E)
  43.  
  44.   Print "I,m back"
  45.   Print I
  46.   Print R
  47.   Print D
  48.   Print (L <> 0)
  49.   Print Left(String@(C), 20)
  50.   Print Single@(E), Single@(E + 4)
  51.  
  52.   Free(C)
  53.   Free(E)
  54.  
  55.  

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!
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
Cedron is in the usergroup ‘Regular’
 After a little digging around, I found out that FORTRAN wants its Booleans as a 4 byte Integer with values restricted to 0 and 1.  I smell some C.

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!
Online now: No Back to the top
1 guest and 0 members have just viewed this.