Fortran to Julia struct

I’m having some difficulty calling some Fortran code from Julia. I have some Fortran code like the following

module fortranjulia
  
  use, intrinsic :: iso_c_binding
  implicit none

  type, bind(c) :: m_t
    integer(c_int)  :: a1
    real(c_double) :: a2(3)
  end type m_t

contains

  subroutine print_mytype(m) bind(C, name = "print_mytype")
    type(m_t), intent(in) :: m
    print *, m%a1
    print *, m%a2
  end subroutine print_mytype

end module

Which I compile using ifort -shared -lifcore -fPIC fortranjulia.f90 -o fortran_julia.so or gfortran -shared -fPIC fortranjulia.f90 -o fortran_julia.so.

On the Julia side I have

using StaticArrays
using Parameters

@with_kw mutable struct m_t
    a1::Cint = 123456
    a2::MVector{3, Cdouble} = [1.0, 2.0, 3.0]
end

m = m_t()
println("Julia")
println(m.a1)
println(m.a2)
println("Fortran")
ccall((:print_mytype, "/path/to/fortran_julia"), Cvoid, (Ref{m_t},), m)

However, the output is mangled for the array when calling the Fortran code.

Julia
123456
[1.0, 2.0, 3.0]
Fortran
      123456
  6.914404822817115E-310  0.000000000000000E+000  6.914470367016270E-310

What am I do wrong with the arrays? Thanks.

1 Like

this needs to be SVector

or alternatively: