Import custom Julia module from C & use it

Hello,

I’m trying to import a local Julia module from C and call a function;
but jl_get_function always return a null pointer.

I based my code on this topic and this SO question, but nothing I’ve tried worked.

This is the C-code :

  /* required: setup the Julia context */
  jl_init();

  {
    printf("include test.jl \n");
    jl_eval_string("Base.include(Main, \"./test.jl\")");
    // Tried both version with no success
    // jl_eval_string("include(\"./test.jl\")");
    printf("using custom_module \n");

    jl_eval_string("using custom_module");
    {
        printf("dummy \n");
        // Call easy function
        jl_function_t *dummy= jl_get_function(jl_main_module, "dummy");

        if (dummy != NULL)
        {
          printf("dummy is not null\n");
        }
        else
        {
          printf("dummy is null\n");
          jl_atexit_hook(0);
          return 0;
        }

        jl_call0(dummy);
        jl_atexit_hook(0);
        return 0;
    } 

This the julia file:

module custom_module 
  using LinearAlgebra

  function dummy()
    println("Hello world from dummy")
  end


  function testMeBaby(A)
    println(typeof(A))
    println(A)
    B = lmul!(10, A)
    println(B)
    println(size(B))
    println(ndims(B))
    return B
  end

  export testMeBaby
  export dummy
end

What am I missing or doing wrong here ?
I’m using gcc 9.3.0 &nd Julia v1.4.2 with Ubuntu 20.04 inside WSL2.

Also Is there any restriction I should be aware of when executing Julia function from C ?

1 Like

You are using jl_eval_string to run julia code, so you need to make sure that actually runs first. Either by just running the julia code, or check the status after calling jl_eval_string.

using .custom_module

Thanks for your help !

Putting a ".’ before the module name do work indeed.
Why is it required ? Is it because it’s a local module ?

Modules · The Julia Language Not really “local”, but they are not toplevel.