# Calling julia functions from c and vice versa

**URL:** <https://discourse.julialang.org/t/calling-julia-functions-from-c-and-vice-versa/117254>\
**Category:** New to Julia\
**Created:** [July 19, 2024, 7:30pm UTC](https://discourse.julialang.org/t/calling-julia-functions-from-c-and-vice-versa/117254 "2024-07-19T19:30:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeterCourcoux](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@PeterCourcoux](https://discourse.julialang.org/u/PeterCourcoux)\
**Post date:** [July 19, 2024, 7:30pm UTC](https://discourse.julialang.org/t/calling-julia-functions-from-c-and-vice-versa/117254/1 "2024-07-19T19:30:16Z")

</div>

I’m new to both Julia and c. I hope what I’m trying to do makes sense! The general idea is to try to use the enlightenment foundation libraries ([enlightenment.org](http://enlightenment.org)) to build UI’s for julia code, so will need to call both ways, c functions from julia and julia functions from c. Baby steps first!

I have the following:  
Julia code:

```julia
#! /usr/bin/env julia

module juliaProcess
function process()
    for i=1:5
        println("i is "*string(i))
    end
end
export process
end

```

c code:

```julia
#define EFL_BETA_API_SUPPORT 1
 
#include <Efl_Ui.h>
#include <julia.h>                                                                                                                                                                               
#include <stdio.h>
JULIA_DEFINE_FAST_TLS

static void
_gui_quit_cb(void *data EINA_UNUSED, const Efl_Event *event EINA_UNUSED)
{
   efl_exit(0);
}
 
static void
_gui_setup()
{
   Eo *win, *box;
 
   win = efl_add(EFL_UI_WIN_CLASS, efl_main_loop_get(),
                 efl_ui_win_type_set(efl_added, EFL_UI_WIN_TYPE_BASIC),
                 efl_text_set(efl_added, "Hello World"),
                 efl_ui_win_autodel_set(efl_added, EINA_TRUE));
 
   efl_event_callback_add(win, EFL_UI_WIN_EVENT_DELETE_REQUEST, _gui_quit_cb, NULL);
 
   box = efl_add(EFL_UI_BOX_CLASS, win,
                efl_content_set(win, efl_added),
                efl_gfx_hint_size_min_set(efl_added, EINA_SIZE2D(360, 240)));
 
   efl_add(EFL_UI_TEXTBOX_CLASS, box,
           efl_text_markup_set(efl_added, "Hello World. This is an <b>Efl.Ui</b> application!"),
           efl_text_interactive_selection_allowed_set(efl_added, EINA_FALSE),
           efl_gfx_hint_weight_set(efl_added, 1.0, 0.9),
           efl_gfx_hint_align_set(efl_added, 0.5, 0.5),
           efl_pack(box, efl_added));
 
   efl_add(EFL_UI_BUTTON_CLASS, box,
           efl_text_set(efl_added, "Quit"),
           efl_gfx_hint_weight_set(efl_added, 1.0, 0.1),
           efl_pack(box, efl_added),
           efl_event_callback_add(efl_added, EFL_INPUT_EVENT_CLICKED,
                                  _gui_quit_cb, efl_added));
}

void julia_main()                                                                                                                                                                                
{                                                                                                                                                                                                  
  jl_init();
  jl_eval_string("Base,include(Main, \"juliaProcess\")");
  jl_eval_string("using Main.juliaProcess");
  jl_module_t* juliaProcess = (jl_module_t*)jl_eval_string("Main.juliaProcess");
  //jl_function_t* processfn = jl_get_function(juliaProcess, "process");
  //jl_value_t* pa = jl_call0(processfn);
  /* run Julia commands */
  jl_eval_string("println(sqrt(2.0))");
  jl_atexit_hook(0);                                                                                                                                                                             
}

 
EAPI_MAIN void
efl_main(void *data EINA_UNUSED, const Efl_Event *ev EINA_UNUSED)
{
   _gui_setup();
   julia_main();
}
EFL_MAIN()

```

I created a pkg-config julia.pc file and then compiled the c code with:

```julia
gcc -o efltest efltest.c `pkg-config --cflags --libs eina efl elementary julia`

```

This compiles and runs and I see a small demo app in a window on my ubuntu desktop as expected. The println(sqrt(2.0)) displays the expected result. The problem comes when I uncomment the following line:

```julia
jl_function_t* processfn = jl_get_function(juliaProcess, "process");

```

I get a seg fault:

```julia
[52270] signal (11.1): Segmentation fault
in expression starting at none:0
jl_get_module_binding at /cache/build/builder-amdci5-1/julialang/julia-release-1-dot-10/src/module.c:721
jl_resolve_owner at /cache/build/builder-amdci5-1/julialang/julia-release-1-dot-10/src/module.c:379 [inlined]
ijl_get_binding at /cache/build/builder-amdci5-1/julialang/julia-release-1-dot-10/src/module.c:464
ijl_get_global at /cache/build/builder-amdci5-1/julialang/julia-release-1-dot-10/src/module.c:775
jl_get_function at ./efltest (unknown line)
julia_main at ./efltest (unknown line)
efl_main at ./efltest (unknown line)
_event_callback_call at /usr/local/lib/x86_64-linux-gnu/libeo.so.1 (unknown line)
efl_event_callback_call at /usr/local/lib/x86_64-linux-gnu/libeo.so.1 (unknown line)
_efl_loop_arguments_send at /usr/local/lib/x86_64-linux-gnu/libecore.so.1 (unknown line)
_efl_future_cb at /usr/local/lib/x86_64-linux-gnu/libeo.so.1 (unknown line)
_eina_future_cb_dispatch at /usr/local/lib/x86_64-linux-gnu/libeina.so.1 (unknown line)
_eina_future_dispatch at /usr/local/lib/x86_64-linux-gnu/libeina.so.1 (unknown line)
_eina_future_dispatch at /usr/local/lib/x86_64-linux-gnu/libeina.so.1 (unknown line)
_futures_dispatch_cb at /usr/local/lib/x86_64-linux-gnu/libeo.so.1 (unknown line)
_event_callback_call at /usr/local/lib/x86_64-linux-gnu/libeo.so.1 (unknown line)
efl_event_callback_call at /usr/local/lib/x86_64-linux-gnu/libeo.so.1 (unknown line)
_ecore_main_loop_iterate_internal at /usr/local/lib/x86_64-linux-gnu/libecore.so.1 (unknown line)
_ecore_main_loop_begin at /usr/local/lib/x86_64-linux-gnu/libecore.so.1 (unknown line)
_efl_loop_begin at /usr/local/lib/x86_64-linux-gnu/libecore.so.1 (unknown line)
efl_loop_begin at /usr/local/lib/x86_64-linux-gnu/libecore.so.1 (unknown line)
main at ./efltest (unknown line)
unknown function (ip: 0x7a946d829d8f)
__libc_start_main at /lib/x86_64-linux-gnu/libc.so.6 (unknown line)
_start at ./efltest (unknown line)
Allocations: 2907 (Pool: 2899; Big: 8); GC: 0

```

Any help or pointers to potential issues will be gratefully received.  
Also, if it is straight forward to call into the c code from Julia an example of how I might achieve that so that I can update the ui in near real time would be really helpful.  
Thanks again, and I hope this is the right place to post this question.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [July 19, 2024, 7:47pm UTC](https://discourse.julialang.org/t/calling-julia-functions-from-c-and-vice-versa/117254/2 "2024-07-19T19:47:32Z")

</div>

> [@PeterCourcoux](#):
>
> ` jl_eval_string("Base,include(Main, \"juliaProcess\")");`

This line does not look right. Were you trying to call `Base.include`? I think you probably want

```julia
jl_eval_string("include(Main, \"juliaProcess\")");

```

That `include` is `Main.include` or canonically `Base.MainInclude.include`.
