# Using compiled Fortran shared library

**URL:** <https://discourse.julialang.org/t/using-compiled-fortran-shared-library/15672>\
**Category:** New to Julia\
**Tags:** fortran\
**Created:** [September 29, 2018, 6:43pm UTC](https://discourse.julialang.org/t/using-compiled-fortran-shared-library/15672 "2018-09-29T18:43:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![korvin42](https://avatars.discourse-cdn.com/v4/letter/k/e480ec/32.png) [@korvin42](https://discourse.julialang.org/u/korvin42)\
**Post date:** [September 29, 2018, 6:43pm UTC](https://discourse.julialang.org/t/using-compiled-fortran-shared-library/15672/1 "2018-09-29T18:43:24Z")

</div>

Hi, everyone!

I’m new to Julia and I’m trying to implement some existing Fortran libraries to use in Julia code.  
However, the `ccall` function does not appear to change the values of variables which I pass to it.

A simplified version of Fortran code is:

```julia
SUBROUTINE TEST (IOPT,PARMOD,PS,X,Y,Z,VX,VY,VZ)
REAL IOPT,PARMOD(10),PS,X,Y,Z,VX,VY,VZ
VX=5.
VY=VX*3.
RETURN
END

```

I compile is to the shared library with:

```julia
gfortran -shared -fPIC test.for -o test.so

```

in Julia when I run yhis code:

```julia
iopt = 0.; ps = π / 180. * 11
parmod = [1., -9., 2.51, 0.97, 0.032, 0.009, 0.193, 0.008, 0.001, 0.004]
x = 3.; y = 0.; z = 0.
Vx = -1e31; Vy = -1e31; Vz = -1e31

ccall((:test_, "./test.so"),
      Cvoid, # output
      (Ref{Float64}, # IOPT, REAL
       Ref{Float64}, # PARMOD, REAL
       Ref{Float64}, # PS, REAL
       Ref{Float64}, # X, REAL
       Ref{Float64}, # Y, REAL
       Ref{Float64}, # Z, REAL
       Ref{Float64}, # Vx, REAL
       Ref{Float64}, # Vy, REAL
       Ref{Float64}), # Vz, REAL
       iopt, parmod, ps, x, y, z, Vx, Vy, Vz)

```

The values of `Vx, Vy, Vz` stay the same. Even though they had to chanhe.

Can someone please help me out with this issue?

---

<div class="post-metadata">

**Author:** ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)\
**Post date:** [September 29, 2018, 7:28pm UTC](https://discourse.julialang.org/t/using-compiled-fortran-shared-library/15672/2 "2018-09-29T19:28:35Z")

</div>

For mutable scalar variables, you need to wrap in `Ref`: see “Calling C and Fortran code: Passing pointers for modifying inputs” in the manual (the description mentions C, but applies to Fortran too). Also note that the default kind for REAL in Fortran is usually Float32.

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [September 29, 2018, 10:29pm UTC](https://discourse.julialang.org/t/using-compiled-fortran-shared-library/15672/3 "2018-09-29T22:29:36Z")

</div>

There are some simple examples here:

> [@Calling fortran function](https://discourse.julialang.org/t/calling-fortran-function/2288/9):
>
> Also here is a simple example: subroutine foo1(n,x,y) integer\*4, intent(in) :: n integer\*4, intent(in), dimension(n) :: x integer\*4, intent(out), dimension(n) :: y y = 2\*x end subroutine foo1 subroutine foo2(n,x,y) integer\*8, intent(in) :: n integer\*8, intent(in), dimension(n) :: x integer\*8, intent(out), dimension(n) :: y y = 2\*x end subroutine foo2 Compile into a shared library, and then in Julia, e.g.: ftest = Libdl.dlopen(…

---

<div class="post-metadata">

**Author:** ![korvin42](https://avatars.discourse-cdn.com/v4/letter/k/e480ec/32.png) [@korvin42](https://discourse.julialang.org/u/korvin42)\
**Post date:** [October 1, 2018, 12:42am UTC](https://discourse.julialang.org/t/using-compiled-fortran-shared-library/15672/4 "2018-10-01T00:42:01Z")

</div>

Thanks for your help.  
@Ralph_Smith pointed me in the right direction.  
Changing the definition of mutable variables to:

```julia
Vx = Ref{Cdouble}(-1e31)
Vy = Ref{Cdouble}(-1e31)
Vz = Ref{Cdouble}(-1e31)

```

resolved the problem.
