# Calling Julia Functions using 'cfunction'

**URL:** <https://discourse.julialang.org/t/calling-julia-functions-using-cfunction/9032>\
**Category:** General Usage\
**Tags:** embedding, c\
**Created:** [February 13, 2018, 12:06pm UTC](https://discourse.julialang.org/t/calling-julia-functions-using-cfunction/9032 "2018-02-13T12:06:16Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![pemryan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pemryan/32/1237_2.png) [@pemryan](https://discourse.julialang.org/u/pemryan)\
**Post date:** [February 13, 2018, 12:06pm UTC](https://discourse.julialang.org/t/calling-julia-functions-using-cfunction/9032/1 "2018-02-13T12:06:16Z")

</div>

Hello,

Besides the examples given in doc ([https://docs.julialang.org/en/stable/manual/embedding/](https://docs.julialang.org/en/stable/manual/embedding/)), I figured out two more methods to call Julia functions in C, using the built-in ‘cfunction’:

```
1) use julia cfunction

```

```julia
    typedef float (*Func)(float);
    jl_value_t *ret = jl_eval_string("cfunction(sqrt, Cfloat, (Cfloat,))"); 
    JL_GC_PUSH1(&ret);
    void *ptr1 = jl_unbox_voidpointer(ret); 
    Func csqrt = (Func)ptr1;
    float r40 = csqrt(2.0f); 
    printf("%f", r40);
    JL_GC_POP();

```

```
2) use jl_function_ptr. this is the function called by 'cfunction' in base/c.jl. It's not provided in Julia.h, just put it somewhere.  

```

`JL_DLLEXPORT void *jl_function_ptr(jl_function_t *f, jl_value_t *rt, jl_value_t *argt);`

```julia
    typedef float (*Func)(float);
    jl_function_t *f = jl_get_function(jl_base_module, "sqrt");
    jl_value_t *rt = jl_eval_string("Cfloat");
    jl_value_t *argt = jl_eval_string("(Cfloat,)"); 
    JL_GC_PUSH3(&f, &rt, &argt);
    void *ptr = jl_function_ptr(f, rt, argt); 
    Func csqrt = (Func)ptr; 
    float r30 = csqrt(2.0f); 
    JL_GC_POP();

```

It works on x86\_64-w64-mingw32 with Julia 0.6.2 pre-built binaries. Other platforms not tested.  
Note : JL\_GC\_PUSH\* introduced an undefined reference to \_\_imp\_jl\_tls\_states with the 0.6.2 Win64 binaries.

`#define JULIA_ENABLE_THREADING 0`

to work around.

Hope this info is helpful.

Regards,  
pem
