# Calling Fortran variables from Julia

**URL:** <https://discourse.julialang.org/t/calling-fortran-variables-from-julia/27293>\
**Category:** New to Julia\
**Tags:** question, fortran\
**Created:** [August 8, 2019, 1:35am UTC](https://discourse.julialang.org/t/calling-fortran-variables-from-julia/27293 "2019-08-08T01:35:32Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![anowacki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/anowacki/32/17375_2.png) [@anowacki](https://discourse.julialang.org/u/anowacki)\
**Post date:** [August 8, 2019, 2:30am UTC](https://discourse.julialang.org/t/calling-fortran-variables-from-julia/27293/3 "2019-08-08T02:30:09Z")

</div>

I don’t know of a reliable way to do this without changing the Fortran source, but this may help if you can.

If you _are_ able to edit your source and it is practical to do so, I recommend using Fortran’s C interoperability (by declaring `use iso_c_binding` in the module before `implicit none`).

For the constants, I would write (or make the computer write if there are many such definitions needed) a wrapper function like so:

```fortran
function get_hparam() bind(c)
  integer(C_INT) :: get_hparam

  get_hparam = int(hparam, C_INT)
end function get_hparam

```

which can be called in Julia like this:

```julia
julia> ccall((:get_hparam, "./simplemodule.so"), Cint, ())
10

```

For allocatable arrays, I believe you have to [declare the array a `pointer` or `target`](http://fortranwiki.org/fortran/show/c_loc). You can then use `c_loc` to get a C pointer to the data. In the Fortran module:

```fortran
function get_s() bind(c)
  type(c_ptr) :: get_s

  get_s = c_loc(s)
end function get_s

```

From Julia:

```julia
julia> ps = ccall((:get_s, "simplemodule.so"), Ptr{Cfloat}, ())
Ptr{Float32} @0x00007ff1254b8d50

julia> s = unsafe_wrap(Vector{Cfloat}, ps, 10)
10-element Array{Float32,1}:
  1.0
  2.0
  3.0
  4.0
  5.0
  6.0
  7.0
  8.0
  9.0
 10.0

```

---

_[View the full topic](https://discourse.julialang.org/t/calling-fortran-variables-from-julia/27293)._
