Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use/create a more generic ccall in Julia?

Tags:

c

julia

I'd like to call a function that calls a c function, but I want to be able to swap out the actual C function

function Ccall(fn)
    ... more code 1 ...    
    ccall((fn, libc), Int32, ()) 
    ... more code 2 ...
end

where the more code part is always the same but fn can change. For a simple example, I try to call this Ccall function

f = "clock" |> Symbol
Ccall(f)

but I get an error message. Also tried

function Ccall(fn)
    ... more code 1 ...    
    ccall(fn, Int32, ()) 
    ... more code 2 ...
end

with

const f = ("clock" |> Symbol, "libc")
Ccall(f)

but no luck, f is not a constant expression, what it should be, just because I put a const in front.

Is there a way of ccall-ing with the above genericity? If so, how?

like image 833
Ferenc Avatar asked Dec 13 '25 02:12

Ferenc


1 Answers

ccall requires an expression, not a value---it's a little bit like a macro in that regard. You can generate the appropriate expressions (see the HDF5.jl package for one example), but your best bet is probably to create one wrapper-function per C function as Gnimuc Key suggested.

like image 173
tholy Avatar answered Dec 14 '25 19:12

tholy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!