I am currently trying to create a unittest for a function I have defined in my shared fortran library. This function receives an integer value and inserts it into a previously allocated and declared array of a specific fortran type (the equivalent to a c++ struct in fortran)
My julia code looks like this
using Base.Libc.Libdl
mutable struct jul_param
M::Float64
dot_gamma_0::Float64
dilatation::Bool
output::String
end
try
damask_bin = Libdl.dlopen("/path/to/my/shared_lib.so")
function_sym = Libdl.dlsym(damask_bin, Symbol("__modnamestart.testmodule_MOD_test_func"))
param_sym = Libdl.dlsym(damask_bin, Symbol("__modnamestart.testmodule_MOD_test_param"))
param_pointer = cglobal(param_sym, Ptr{jul_param})
testparam = jul_param(1.0, 2.0, true, "output1")
ptr = ccall((:malloc, "/usr/lib/x86_64-linux-gnu/libc.so.6"), Ptr{jul_param}, (Csize_t,), sizeof(jul_param) * 2)
unsafe_store!(ptr,testparam,1)
println(unsafe_load(ptr,1)) #testparam(1.0, 2.0, true, "output1")
err = ccall(function_sym,
Cvoid,
(Ref{Int64},Ref{Int64}),
1,2)
catch e
println("Error: ", e)
end
And my Fortran code currently looks like this:
module testmodule
type :: ParamsTest
real(pReal) :: &
M, &
dot_gamma_0
logical :: &
dilatation
character(len=pStringLen), allocatable, dimension(:) :: &
output
end type ParamsTest
type(ParamsTest), allocatable, dimension(:) :: test_param
contains
module subroutine test_func(index, test)
integer, intent(in) :: &
index, test
print *, "input", index, test
print *, "array", test_param(ph)%output
end subroutine test_func
end module testmodule
However, this results in a segmentation fault in the fortran line print *, "array", test_param(ph)%output
. I am currently using malloc from libc to allocate memory for my struct, is the problem possibly related to this? How can I fix my julia function to accomplish what I want?