# I want to embed a Julia module inside a C program. The documentation is not very c

**URL:** <https://discourse.julialang.org/t/i-want-to-embed-a-julia-module-inside-a-c-program-the-documentation-is-not-very-c/54928>\
**Category:** General Usage\
**Created:** [February 9, 2021, 2:04pm UTC](https://discourse.julialang.org/t/i-want-to-embed-a-julia-module-inside-a-c-program-the-documentation-is-not-very-c/54928 "2021-02-09T14:04:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BridgeBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bridgebot/32/21491_2.png) [@BridgeBot](https://discourse.julialang.org/u/BridgeBot)\
**Post date:** [February 9, 2021, 2:04pm UTC](https://discourse.julialang.org/t/i-want-to-embed-a-julia-module-inside-a-c-program-the-documentation-is-not-very-c/54928/1 "2021-02-09T14:04:56Z")

</div>

I want to embed a Julia module inside a C program. The documentation is not very clear. Does someone know how to import a module? The example uses `jl_function_t *func = jl_get_function(jl_base_module, "sqrt");` and I would like to do something like  
`jl_function_t *myfunc = jl_get_function(jl_mymodule_module, "myfunc");`

Note that the original poster on Slack cannot see your response here on Discourse. Consider _transcribing the appropriate answer back to Slack_, or pinging the poster here on Discourse so they can _follow this thread_.  
[(Original message :slack:)](https://julialang.slack.com/archives/C6A044SQH/p1612571094142500?thread_ts=1612571094.142500&cid=C6A044SQH) [(More Info)](https://github.com/JuliaCommunity/SlackBridge)

---

<div class="post-metadata">

**Author:** ![Jorge\_Vieyra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jorge_vieyra/32/6527_2.png) [@Jorge\_Vieyra](https://discourse.julialang.org/u/Jorge_Vieyra)\
**Post date:** [February 9, 2021, 2:10pm UTC](https://discourse.julialang.org/t/i-want-to-embed-a-julia-module-inside-a-c-program-the-documentation-is-not-very-c/54928/2 "2021-02-09T14:10:32Z")

</div>

I got answers from @mbauman and Xirui Zhao. Got these good references:

small breadcrumb: [How to use Julia special functions inside c++ - Stack Overflow](https://stackoverflow.com/questions/65322339/how-to-use-julia-special-functions-inside-c#comment115494570_65322339)

Perhaps you need to call jl\_load [https://github.com/JuliaLang/julia/blob/985158ff5b138954c87db273ebfed2ca4321a67c/src/toplevel.c#L1051](https://github.com/JuliaLang/julia/blob/985158ff5b138954c87db273ebfed2ca4321a67c/src/toplevel.c#L1051), also see [Eval of Julia code · The Julia Language](https://docs.julialang.org/en/v1/devdocs/eval/)

Although my recommendation is to write your C program as a library and call it from Julia. You can pass Julia function pointers to C functions. [Calling C and Fortran Code · The Julia Language](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/#Creating-C-Compatible-Julia-Function-Pointers).

---

<div class="post-metadata">

**Author:** ![Jorge\_Vieyra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jorge_vieyra/32/6527_2.png) [@Jorge\_Vieyra](https://discourse.julialang.org/u/Jorge_Vieyra)\
**Post date:** [February 9, 2021, 2:11pm UTC](https://discourse.julialang.org/t/i-want-to-embed-a-julia-module-inside-a-c-program-the-documentation-is-not-very-c/54928/3 "2021-02-09T14:11:35Z")

</div>

I modified the example from the post from SGJ that @mbauman referred me to. I want to modify it in order to send back and forth arrays with zero copy. However, I don’t know what is missing. I’m sure that it is something silly.

```julia
#include <julia.h>
JULIA_DEFINE_FAST_TLS() // only define this once, in an executable (not in a shared library) if you want fast code.

typedef jl_array_t* (*erf_ptr)(jl_array_t * z);
// erf_ptr erf = NULL;

#include <iostream>
int main(int argc, char *argv[])
{
    jl_init();
    jl_value_t* array_type = jl_apply_array_type((jl_value_t*)jl_float64_type, 1);
    double v[] = { .1, .2, .3, .4 };

    // Expected output
    // julia> [.1, .2, .3, .4] .|> SpecialFunctions.erf
    // 4-element Vector{Float64}:
    // 0.1124629160182849
    // 0.22270258921047847
    // 0.3286267594591274
    // 0.42839235504666845

    jl_array_t *x = jl_ptr_to_array_1d(array_type, v, 4, 0);
    // get a C function pointer to the Julia erf function compiled for
    jl_eval_string("import SpecialFunctions");
    jl_value_t *ret = jl_eval_string("@cfunction(x->SpecialFunctions.erf.(x), Vector{Float64},(Vector{Float64},))");
    erf_ptr erf = (erf_ptr) jl_unbox_voidpointer(ret);

    jl_array_t* out = erf(x); // Probably doing something wrong here
    double *outData = (double*)jl_array_data(out);
        // Print out the vector
    for(size_t i=0; i<4; i++)
        std::cout << outData[i] << ' ';
    std::cout << "\n";
    // Returns garbage
    // 4.94066e-324 0 0 6.91799e-310 
    jl_atexit_hook(0);
    return 0;
}

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [February 9, 2021, 2:25pm UTC](https://discourse.julialang.org/t/i-want-to-embed-a-julia-module-inside-a-c-program-the-documentation-is-not-very-c/54928/4 "2021-02-09T14:25:53Z")

</div>

I would suggest not wrapping your array in a Julia array from the C side but let the Julia code handle the raw data. Cf [https://github.com/GunnarFarneback/DynamicallyLoadedEmbedding.jl/pull/3/commits/75bc6fe566e07b6635685f2d0ee77dad931bd68d](https://github.com/GunnarFarneback/DynamicallyLoadedEmbedding.jl/pull/3/commits/75bc6fe566e07b6635685f2d0ee77dad931bd68d)
