Using compiled Fortran shared library

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:

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:

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

in Julia when I run yhis code:

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?

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.

2 Likes

There are some simple examples here:

1 Like

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

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

resolved the problem.