I am not being able to properly pass a julia-defined function that takes vector and scalar values to C-defined functions.
I have two functions in C
EXPORT_C double apply_fun_simple(double (*fun)(double*), double* x) {
return (*fun)(x);
}
EXPORT_C double apply_fun_mixed(double (*fun)(double*, size_t), double* x, size_t len) {
return (*fun)(x,len);
}
and my julia code is
using Libdl
Libdl.dlopen("/path/to/libDummy.so")
# SIMPLE
function foo(x::Vector{Float64})::Float64
return sum(x)
end
x = Vector{Float64}([1,2,3])
foo_c = @cfunction(foo, Float64, (Ref{Float64},))
r_foo = @ccall :libDummy.apply_fun_simple(foo_c::Ptr{Cvoid}, x::Ref{Float64})::Float64
println(r_foo)
# MIXED
function bar(x::Vector{Float64}, s::UInt64)::Float64
return sum(x[i] for i in 1:s)
end
x = Vector{Float64}([1,2,3])
s::UInt64 = length(x)
bar_c = @cfunction(bar, Float64, (Ref{Float64},UInt64,))
r_bar = @ccall :libDummy.apply_fun_mixed(bar_c::Ptr{Cvoid}, x::Ref{Float64}, s::UInt64)::Float64
println(r_bar)
but I get
ERROR: LoadError: MethodError: no method matching bar(::Float64, ::UInt64)
at the ccall macro.
I have tried different types for both variables, but I’ve had no success. Note that the call to apply_fun_simple
works, but the call to apply_fun_mixed
doesn’t!