# Passing function pointer to Cxx

**URL:** <https://discourse.julialang.org/t/passing-function-pointer-to-cxx/1159>\
**Category:** General Usage\
**Tags:** cxx\
**Created:** [December 27, 2016, 8:22am UTC](https://discourse.julialang.org/t/passing-function-pointer-to-cxx/1159 "2016-12-27T08:22:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [December 27, 2016, 8:22am UTC](https://discourse.julialang.org/t/passing-function-pointer-to-cxx/1159/1 "2016-12-27T08:22:02Z")

</div>

I am using the [Cxx.jl](https://github.com/Keno/Cxx.jl) to wrap a C++ library. Now the C++ library has some methods that take function pointers as arguments. Is there a recommended way to wrap such a function? As a minimal example say I want to wrap

```julia
using Cxx
cxx"""
void evaluate(void (*f)() ){
    f();
}
"""

```

I could pass a julia function by doing

```julia
function evaluate(f_jl)
    cxx"void f_xx(){$f_jl();}"
    icxx"evaluate(&f_xx);"
end

```

```julia
f() = println("heyho")
evaluate(f)
heyho

```

However this does not allow to pass another function later on:

```julia
g() = println("heyho")
evaluate(g)

LoadError: Clang did not create a global variable for the given VarDecl
while loading In[3], in expression starting on line 3

 in SetDeclInitializer(::Cxx.ClangCompiler, ::Cxx.CppPtr{Cxx.CxxQualType{Cxx.CppBaseType{Symbol("clang::VarDecl")},(false,false,false)},(false,false,false)}, ::Cxx.CppPtr{Cxx.CxxQualType{Cxx.CppBaseType{Symbol("llvm::Constant")},(false,false,false)},(false,false,false)}) at /home/admin/.julia/v0.5/Cxx/src/cxxstr.jl:35
 in ArgCleanup(::Cxx.ClangCompiler, ::Function, ::Cxx.CppPtr{Cxx.CxxQualType{Cxx.CppBaseType{Symbol("clang::VarDecl")},(false,false,false)},(false,false,false)}) at /home/admin/.julia/v0.5/Cxx/src/cxxstr.jl:191
 in macro expansion at /home/admin/.julia/v0.5/Cxx/src/cxxstr.jl:661 [inlined]
 in evaluate(::Function) at ./In[1]:11

```

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [December 27, 2016, 10:56am UTC](https://discourse.julialang.org/t/passing-function-pointer-to-cxx/1159/2 "2016-12-27T10:56:52Z")

</div>

I found a way that seems to work, however I am still not sure if this is a sane solution?

```julia
function evaluate(f)
    f_void_ptr = cfunction(f, Void, ()) # void pointer on f. Need to cast it to function pointer
    icxx"""
    typedef void (*fptr)();
    fptr f_fun_ptr = reinterpret_cast<fptr>(reinterpret_cast<long>($f_void_ptr));
    evaluate(f_fun_ptr);
    """
end

```

```julia
f() = println("hi")
evaluate(f)
hi

```

```julia
g() = println("ho")
evaluate(g)
ho

```

---

<div class="post-metadata">

**Author:** ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)\
**Post date:** [December 27, 2016, 1:29pm UTC](https://discourse.julialang.org/t/passing-function-pointer-to-cxx/1159/3 "2016-12-27T13:29:39Z")

</div>

`cfunction` should be the way to go, yes, but I don’t understand why you need to cast to `long` first?

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [December 27, 2016, 3:16pm UTC](https://discourse.julialang.org/t/passing-function-pointer-to-cxx/1159/4 "2016-12-27T15:16:08Z")

</div>

Thanks! I guess the `long` cast is not necessary.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 27, 2016, 4:50pm UTC](https://discourse.julialang.org/t/passing-function-pointer-to-cxx/1159/5 "2016-12-27T16:50:36Z")

</div>

> [@jw3126](#):
>
> Thanks! I guess the long cast is not necessary.

It’s not only not necessary, it’s actively harmful: it will break e.g. on 64-bit Windows, where `long` if 4 bytes while pointers are 8 bytes.

---

<div class="post-metadata">

**Author:** ![groot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/groot/32/13081_2.png) [@groot](https://discourse.julialang.org/u/groot)\
**Post date:** [December 1, 2018, 9:36pm UTC](https://discourse.julialang.org/t/passing-function-pointer-to-cxx/1159/6 "2018-12-01T21:36:35Z")

</div>

For anyone that is interested doing this

```julia
using Cxx

cxx"""
void evaluatec(void (*f)() ){
    f();
}
"""

function evaluate(f)
    f_void_ptr = @cfunction($f, Cvoid, ()) # void pointer on f. Need to cast it to function pointer
    f_void_ptr2 = f_void_ptr.ptr
    #show(typeof(f_void_ptr))
    icxx"""
    typedef void (*fptr)();
    fptr f_fun_ptr = reinterpret_cast<fptr>($f_void_ptr2);
    evaluatec(f_fun_ptr);
    """
end

f() = println("hi")
evaluate(f)

g() = print("ho")
evaluate(g)

```

works under 1.0.2

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [December 2, 2018, 10:58pm UTC](https://discourse.julialang.org/t/passing-function-pointer-to-cxx/1159/7 "2018-12-02T22:58:32Z")

</div>

Cxx.jl works under julia 1.0.2? Cool 🥳

---

<div class="post-metadata">

**Author:** ![groot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/groot/32/13081_2.png) [@groot](https://discourse.julialang.org/u/groot)\
**Post date:** [December 2, 2018, 11:55pm UTC](https://discourse.julialang.org/t/passing-function-pointer-to-cxx/1159/8 "2018-12-02T23:55:38Z")

</div>

Yes, if you complie julia from source 😆
