# Jl\_get\_function() throwing Segmentation fault

**URL:** <https://discourse.julialang.org/t/jl-get-function-throwing-segmentation-fault/37186>\
**Category:** General Usage\
**Tags:** embedding\
**Created:** [April 7, 2020, 6:30pm UTC](https://discourse.julialang.org/t/jl-get-function-throwing-segmentation-fault/37186 "2020-04-07T18:30:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndresGomez97](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@AndresGomez97](https://discourse.julialang.org/u/AndresGomez97)\
**Post date:** [April 7, 2020, 6:30pm UTC](https://discourse.julialang.org/t/jl-get-function-throwing-segmentation-fault/37186/1 "2020-04-07T18:30:22Z")

</div>

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:

```julia
jl_init();

jl_eval_string("include(\"function.jl\")");
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);
jl_atexit_hook(0);

```

function.jl file:

```julia
module Fun

add(x,y) = x+y

end

```

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():

```julia
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](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.

---

<div class="post-metadata">

**Author:** ![AndresGomez97](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@AndresGomez97](https://discourse.julialang.org/u/AndresGomez97)\
**Post date:** [April 9, 2020, 4:34pm UTC](https://discourse.julialang.org/t/jl-get-function-throwing-segmentation-fault/37186/2 "2020-04-09T16:34:21Z")

</div>

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:

```julia
jl_init();
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);
jl_atexit_hook(0);

```

dlsyming every method from libjulia before this.

---

<div class="post-metadata">

**Author:** ![boogeyman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/boogeyman/32/20328_2.png) [@boogeyman](https://discourse.julialang.org/u/boogeyman)\
**Post date:** [December 16, 2020, 5:04pm UTC](https://discourse.julialang.org/t/jl-get-function-throwing-segmentation-fault/37186/3 "2020-12-16T17:04:59Z")

</div>

Hi,  
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++?
