Jl_get_function() throwing Segmentation fault

I was trying to use jl_get_function() julia.h method to call a method implemented on a different .jl file in julia, being both files in the same directory and on Julia1.3.1 .

C file:


jl_value_t *mod = jl_eval_string("Fun");
jl_function_t *add = jl_get_function((jl_module_t*)mod,"add");

jl_value_t *args[2];
args[0] = jl_box_int32(6);
args[1] = jl_box_int32(6);
int *res = jl_unbox_int32(jl_call(add,args,2));

printf("Res: %d\n",res);

function.jl file:

module Fun

add(x,y) = x+y


When I run my c file called get function.c this occurs, and because of debug prints I used I can tell it occurs when calling jl_get_function():

signal (11): Segmentation fault
in expression starting at none:0
unknown function (ip: 0xffffffffffffffff)
__libc_start_main at /lib/x86_64-linux-gnu/libc.so.6 (unknown line)
unknown function (ip: 0xffffffffffffffff)
Allocations: 2451 (Pool: 2442; Big: 9); GC: 0
Segmentation fault

I checked this example file https://github.com/JuliaLang/julia/blob/master/test/embedding/embedding.c
and I’ve notice that it uses jl_main_module when calling jl_get_function() as module argument, I’ve also tried that but the same error occurred.
Any idea on what I do wrong? I can’t figure out because there is not that many documentation about calling your own implemented methods using jl_get_function so I hope someone can help me.

For future users that encounter the same problem:

If you dlopen libjulia when embedding there will be some symbols that won’t be dlsymed as jl_get_function().

Instead of doing what every documentation suggests, at least in Julia v1.3.1. , you need to use jl_get global() as I do here:

jl_value_t *mod = jl_eval_string("Base");
jl_function_t *sqrt = (jl_function_t*)jl_get_global((jl_module_t*)mod,jl_symbol("sqrt"));

jl_value_t* argument = jl_box_float64(2.0);
jl_value_t* ret = jl_call1(sqrt, argument);
double retDouble = jl_unbox_float64(ret);
printf("sqrt(2.0) in C: %e\n", retDouble);

dlsyming every method from libjulia before this.

1 Like

on a similar issue, I do not see how your original problem is solved here. Nonetheless do you think with your solution I can incorporate SpecialFunctions say gamma(z) in c++?