Calling a Julia function from C++ that takes a Julia callback function as an argument

107 views
Skip to first unread message

Jeff

unread,
Jul 5, 2016, 7:02:23 PM7/5/16
to julia-users

Hi all,


I'd like to call a Julia function from C++. This Julia function takes another Julia function as an argument (a callback function). I'd like to write this callback function entirely in C++ too, and pass it directly to Julia, without declaring a global name for it in Julia's name space (like how ints, floats, etc. are passed from C++ to Julia wo creating top-level names for them in Julia). Presumably the arguments to my C++ implementation of the callback function will havejl_value_t * as their types.

Can anyone show me how to do this?


I tried posting this question on stackoverflow originally but I didn't get an answer.

https://stackoverflow.com/questions/38135749/calling-a-julia-function-from-c-that-takes-a-julia-callback-function-as-an-arg


Thanks much!


Jeff


Isaiah Norton

unread,
Jul 5, 2016, 11:34:52 PM7/5/16
to julia...@googlegroups.com
Here's an extended example of an API that is designed specifically to expose Julia functions to be called from C and Python:


As far as defining functions without "names", on 0.5-dev anonymous functions work fine with `cfunction`:

julia> entry_ptr = cfunction( (x, ptr) -> ccall(ptr, Int, (Int,), x), Int, (Int, Ptr{Void}) )
Ptr{Void} @0x0000000317ee4c60

julia> cb_ptr = cfunction( y -> y^2, Int, (Int,))
Ptr{Void} @0x0000000317ee80c0

julia> myf(x) = ccall(entry_ptr, Int, (Int, Ptr{Void}), x, cb_ptr)
myf (generic function with 1 method)

julia> myf(4)
16
Reply all
Reply to author
Forward
0 new messages