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:
// 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:
Also the “module” is:
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:
$ 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.