Ok so I have a few simple examples working for ccalling fortran functions 
and subroutines from Julia.
Maybe someone will find this useful examples when first looking into 
calling fortran from julia.

compile the following fortran module with

*gfortran simplemodule.f95 -o simplemodule.so -shared -fPIC*


```
!fileName = simplemodule.f95
module simpleModule

implicit none

contains
function foo(x)
  integer :: foo, x
  foo = x * 2
end function foo

subroutine bar(x, a, b)
  integer, intent(in) :: x
  integer, intent(out) :: a, b
  
  a = x + 3
  b = x * 3
end subroutine bar

subroutine keg(x, a, b)
  real*8, intent(in) :: x
  real*8, intent(out) :: a, b
  
  a = x + 3.0
  b = x * 3.0
end subroutine keg

subroutine ruf(x, y)
  real*8, dimension(3), intent(in) :: x
  real*8, dimension(3), intent(out) :: y
  integer :: i
  
  DO i = 1, 3
    y(i) = 2*x(i)
  END DO
end subroutine ruf

end module simplemodule
```

then you can use the following julia script to call the functions from the 
shared library.

```
x1 = 7
a1 = [0]
b1 = [0]

r1 = ccall((:__simplemodule_MOD_foo, 
"/home/abieler/testPrograms/fortranShardLib/simplemodule.so"), Int64, 
            (Ptr{Int64},), &x1)

println(r1)
println()

ccall((:__simplemodule_MOD_bar, 
"/home/abieler/testPrograms/fortranShardLib/simplemodule.so"), Void, 
      (Ptr{Int64}, Ptr{Int64}, Ptr{Int64}), &x1, a1, b1)
      
println(a1[1])
println(b1[1])
println()

x2 = 7.0
a2 = Cdouble[1.0]
b2 = Cdouble[1.0]

ccall((:__simplemodule_MOD_keg, 
"/home/abieler/testPrograms/fortranShardLib/simplemodule.so"), Void, 
      (Ptr{Float64}, Ptr{Float64}, Ptr{Float64}), &x2, a2, b2)
      
println(a2[1])
println(b2[1])
println()

x3 = [1.0, 2.0, 3.0]
y3 = [0.0, 0.0, 0.0]
ccall((:__simplemodule_MOD_ruf, 
"/home/abieler/testPrograms/fortranShardLib/simplemodule.so"), Void, 
      (Ptr{Float64}, Ptr{Float64}), x3, y3)
      
println(y3)
```

Reply via email to