# Error calling Fortran from Julia

**URL:** https://discourse.julialang.org/t/error-calling-fortran-from-julia/55292
**Category:** General Usage
**Tags:** question
**Created:** [February 15, 2021, 2:29am UTC](https://discourse.julialang.org/t/error-calling-fortran-from-julia/55292 "2021-02-15T02:29:19Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Siel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/siel/32/21932_2.png) [@Siel](https://discourse.julialang.org/u/Siel)
#### Post date: [February 15, 2021, 2:29am UTC](https://discourse.julialang.org/t/error-calling-fortran-from-julia/55292/1 "2021-02-15T02:29:19Z")

</div>

I’ve been trying to make a very simple example of Julia calling fortran to work but it seems I’m missing something, right now what I’ve done is:

Fortran code:

```julia
module ipmf
  use, intrinsic :: iso_c_binding
  implicit none
  private
  public :: wavg_f
subroutine wavg_f(x, w, n, wavg) bind(C, name="wavg_f_")

  integer(kind = c_int), intent(in), value :: n !Length of x and w
  real(kind = c_double), intent(in), dimension(n) :: x !Vector of values
  real(kind = c_double), intent(in), dimension(n) :: w !Vector of weights
  real(kind = c_double), intent(out) :: wavg !output
  real(kind = c_double) :: wsum !Sum of weights
  integer :: i !Internal count

  wavg = 0.0
  wsum = 0.0

  do i = 1, n
      wsum = wsum + w(i)
  end do

  do i = 1, n
      wavg = wavg + x(i)*w(i)/wsum
  end do

end subroutine wavg_f
end module ipmf

```

Compiled the code using:

```julia
> gfortran -shared -fPIC ipmf.f95 -o ipmf.so

```

and finally, my Julia code looks like this:

```julia
using Libdl
lib = Libdl.dlopen("libs/ipmf.so")
sym = Libdl.dlsym(lib, :wavg_f_)
x = Float64[1,2,3]
w = Float64[1,1,1]
n = 3
wavg = 0.0
ccall(sym, Cvoid, (Ref{Float64}, Ref{Float64}, Ref{Int64}, Ref{Float64}), x, w, Ref(n), Ref(wavg))
    

```

but instead of getting wavg = 2.0, I’m getting this:

```julia
julia> wavg
0.0

```

I’m sure I’m doing something wrong, I do not have that much experience using Julia… I’d appreciate any help. Thanks!

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [February 15, 2021, 3:06am UTC](https://discourse.julialang.org/t/error-calling-fortran-from-julia/55292/2 "2021-02-15T03:06:02Z")

</div>

> [@Siel](#):
>
> `Ref{Int64}`

Your Fortran code declares this parameter as `integer(kind = c_int)`, which would correspond to `Ref{Cint}` (`Cint == Int32`, a 32-bit integer) not `Int64`.

Also, the array arguments `x` and `w` should probably be declared as `Ptr{Float64}` (or equivalently `Ptr{Cdouble}`) rather than `Ref` (which is used to wrap scalars in pointers).

---

<div class="post-metadata">

### Author: ![Siel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/siel/32/21932_2.png) [@Siel](https://discourse.julialang.org/u/Siel)
#### Post date: [February 15, 2021, 3:24am UTC](https://discourse.julialang.org/t/error-calling-fortran-from-julia/55292/3 "2021-02-15T03:24:08Z")

</div>

Thank you for your answer, I tried some variatios of what you suggest, but I keep getting the same value or (sometimes) I get this error:

```julia
julia> ccall(sym, Cvoid, (Ptr{Cdouble}, Ptr{Cdouble}, Ref{Cint}, Ref{Cdouble}), x, w, Ref(n), Ref(wavg))
ERROR: ReadOnlyMemoryError()
Stacktrace:
 [1] top-level scope at ./REPL[28]:

```

My code right now looks like this:

```julia
using Libdl
lib = Libdl.dlopen("libs/ipmf.so")
sym = Libdl.dlsym(lib, :wavg_f_)
x = Cdouble[1,2,3]
w = Cdouble[1,1,1]
n = convert(Cint, 3)
wavg = 0.0::Cdouble
ccall(sym, Cvoid, (Ptr{Cdouble}, Ptr{Cdouble}, Ref{Cint}, Ref{Cdouble}), x, w, Ref(n), Ref(wavg))

```

---

<div class="post-metadata">

### Author: ![Siel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/siel/32/21932_2.png) [@Siel](https://discourse.julialang.org/u/Siel)
#### Post date: [February 15, 2021, 3:30am UTC](https://discourse.julialang.org/t/error-calling-fortran-from-julia/55292/4 "2021-02-15T03:30:35Z")

</div>

I also created an smaller example avoiding vectors… but somehow I’m making the same mistake:

Fortran code:

```julia
subroutine sum(a, b, c) bind(C, name = "sum_")
  real(kind = c_double), intent(in) :: a, b 
  real(kind = c_double), intent(out) :: c
  c = 0.0
  c = a+b
end subroutine sum

```

and my Julia Code:

```julia
lib = Libdl.dlopen("libs/ipmf.so")
sym_sum = Libdl.dlsym(lib, :sum_)
a=1.0
b=2.0
c=0.0
ccall(sym_sum, Cvoid, (Ref{Cdouble}, Ref{Cdouble}, Ref{Cdouble}), Ref{Cdouble}(a), Ref{Cdouble}(b), Ref{Cdouble}(c))

```

And I’m getting

```julia
julia> c
0.0

```

Instead of 3.0

---

<div class="post-metadata">

### Author: ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)
#### Post date: [February 15, 2021, 9:17am UTC](https://discourse.julialang.org/t/error-calling-fortran-from-julia/55292/5 "2021-02-15T09:17:56Z")

</div>

In this example, `c` is not changed because it is never passed to the Fortran function (you are passing a `Ref` that is initialized to the same value as `c`, but isn’t related to `c` in any other way). Define `c = Ref{Cdouble}(0.0)` from the start and use that in the `ccall`.

---

<div class="post-metadata">

### Author: ![Siel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/siel/32/21932_2.png) [@Siel](https://discourse.julialang.org/u/Siel)
#### Post date: [February 15, 2021, 4:17pm UTC](https://discourse.julialang.org/t/error-calling-fortran-from-julia/55292/6 "2021-02-15T16:17:17Z")

</div>

Thank you @sostock, with your suggestion I finally got some numbers out of fortran. Somehow I kept having problems reading integers. Everytime I wanted to print them I got trash. I had to change the way I was sending them in the Julia code and now is working

```julia
using Libdl
lib = Libdl.dlopen("libs/ipmf.so")
sym = Libdl.dlsym(lib, :wavg_f_)
x = Cdouble[1.0,2.0,3.0]
w = Cdouble[1.0,1.0,1.0]
n = 3
wavg = Ref{Cdouble}(50.0)
ccall(sym, Cvoid, (Ref{Cdouble}, Ref{Cdouble}, Cint, Ref{Cdouble}), x, w, n, wavg)

```

For reasons I don’t yet understand Ref{Cint} / Ref(c) wasn’t passed through properly and because I’m using that variable to define my arrays it make everything crash.

Thank you very much for your help

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [February 15, 2021, 4:45pm UTC](https://discourse.julialang.org/t/error-calling-fortran-from-julia/55292/7 "2021-02-15T16:45:01Z")

</div>

> [@Siel](#):
>
> `subroutine wavg_f(x, w, n, wavg) bind(C, name="wavg_f_")`

Since you are using `bind(C, ...)` then scalar `in` values are passed through using C calling conventions, not as pointers as would happen for Fortran functions… So `n` should be passed as `Cint`, not as `Ref{Cint}`.
