# Calling my own Julia functions from C

**URL:** https://discourse.julialang.org/t/calling-my-own-julia-functions-from-c/98617
**Category:** General Usage
**Tags:** embedding
**Created:** [May 10, 2023, 2:12pm UTC](https://discourse.julialang.org/t/calling-my-own-julia-functions-from-c/98617 "2023-05-10T14:12:46Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![mb96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mb96/32/45786_2.png) [@mb96](https://discourse.julialang.org/u/mb96)
#### Post date: [May 10, 2023, 2:12pm UTC](https://discourse.julialang.org/t/calling-my-own-julia-functions-from-c/98617/1 "2023-05-10T14:12:46Z")

</div>

Hi everyone,

I have been trying to call a user defined function in Julia in a very simple C code and I am not able too. The code builds up from the most simple Julia function call to what I actually want which is calling a function of my own module and in this last part its where it fails.

The C code is the following:

```julia

// very simple C code to call a Julia program 

#include <stdio.h>
#include <julia.h>

int main(){

    jl_init();

    // calling a Julia Base function 
    jl_eval_string("x = sqrt(3.0)");
    jl_eval_string("println(x)");

    // converting types
    jl_value_t *ret = jl_eval_string("sqrt(2.0)");
    double ret_unboxed = jl_unbox_float64(ret);
    printf("sqrt(2.0) in C: %e \n", ret_unboxed);

    // Calling julia functions from Base module
    jl_function_t *func = jl_get_function(jl_base_module, "sqrt");
    jl_value_t *argument = jl_box_float64(2.0);
    jl_value_t *ret2 = jl_call1(func, argument);

    // Calling julia functions from non base modules
    jl_eval_string("using CSV");
    jl_module_t* CSV = (jl_module_t *)jl_eval_string("CSV");
    jl_function_t *write = jl_get_function(CSV, "write");

    // Calling Julia functions from own programs
    jl_eval_string("Base.include(Main, string(/users/miguelborrero/Dropbox/energy_transiitions/Miguel/Code/test.jl))");
    jl_eval_string("using Main.test");
    jl_module_t* test = (jl_module_t *)jl_eval_string("Main.test");
    jl_function_t *test_func = jl_get_function(test, "test_func"); 

    jl_atexit_hook(0);
    return 0;
}

```

The output is:

 ![Captura de Pantalla 2023-05-10 a la(s) 10.09.33](https://global.discourse-cdn.com/julialang/original/3X/5/e/5e439dbf753538ccd0262582d3e399bb7bd5f22c.png)

Also the “module” is:

```julia
module test
using CSV, DataFrames
export test_func

function test_func()
    df = DataFrame(x = [], y = [])
    CSV.write("/users/miguelborrero/Dropbox/energy_transiitions/Miguel/Data/test_C.csv", df)
    return 0.0
end

end

```

A note on compiling the C code:

```julia
$ gcc -o test -fPIC -I$JULIA_DIR/include/julia -L$JULIA_DIR/lib -Wl,-rpath,$JULIA_DIR/lib test_jl2.c -ljulia

```

Any help would be very much appreciated, thanks in advance!  
Miguel.
