# How to call C++ member function in julia?

**URL:** https://discourse.julialang.org/t/how-to-call-c-member-function-in-julia/24104
**Category:** General Usage
**Created:** [May 11, 2019, 7:29am UTC](https://discourse.julialang.org/t/how-to-call-c-member-function-in-julia/24104 "2019-05-11T07:29:07Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![alligatorme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alligatorme/32/6962_2.png) [@alligatorme](https://discourse.julialang.org/u/alligatorme)
#### Post date: [May 11, 2019, 7:29am UTC](https://discourse.julialang.org/t/how-to-call-c-member-function-in-julia/24104/1 "2019-05-11T07:29:07Z")

</div>

by using ccall and function address, the global function in C++ can be called. But how about member function? Calling member function needs the function pointer and object reference both, it seems that ccall cannot make it in julia.

---

<div class="post-metadata">

### Author: ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)
#### Post date: [May 11, 2019, 8:28am UTC](https://discourse.julialang.org/t/how-to-call-c-member-function-in-julia/24104/2 "2019-05-11T08:28:28Z")

</div>

I don’t know if you know these packages, but these solve your problem: [Cxx.jl](https://github.com/JuliaInterop/Cxx.jl) and [CxxWrap.jl](https://github.com/JuliaInterop/CxxWrap.jl).

---

<div class="post-metadata">

### Author: ![alligatorme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alligatorme/32/6962_2.png) [@alligatorme](https://discourse.julialang.org/u/alligatorme)
#### Post date: [May 12, 2019, 1:55am UTC](https://discourse.julialang.org/t/how-to-call-c-member-function-in-julia/24104/3 "2019-05-12T01:55:42Z")

</div>

yeap, CxxWrap.jl can work. But it just wraps the class in C++ and then exposes it to julia and later all works in julia. But my condition is: the object still in C++ side, may I send a order to ask the object to run its member functions(there are dozens of similar functions, I have to deal with them in julia side due to its excellent sytanx). Since my codes runs from C++, inside I jl\_init() the julia and deal sth, according to the results, the inside julia need call back the C++ side member function.

---

<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: [May 12, 2019, 10:27am UTC](https://discourse.julialang.org/t/how-to-call-c-member-function-in-julia/24104/4 "2019-05-12T10:27:52Z")

</div>

This can be done in CxxWrap, using the `method` function on the C++ side. This generates functions that can be called from Julia, with the C++ object as first argument.

---

<div class="post-metadata">

### Author: ![alligatorme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alligatorme/32/6962_2.png) [@alligatorme](https://discourse.julialang.org/u/alligatorme)
#### Post date: [May 12, 2019, 12:47pm UTC](https://discourse.julialang.org/t/how-to-call-c-member-function-in-julia/24104/5 "2019-05-12T12:47:54Z")

</div>

Where is the C++ object you mentioned above created? What I need is created in C++ side. If yes, how to send it to julia side, by a void pointer?

---

<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: [May 12, 2019, 9:29pm UTC](https://discourse.julialang.org/t/how-to-call-c-member-function-in-julia/24104/6 "2019-05-12T21:29:47Z")

</div>

Yes, objects are created on the C++ side and passed to Julia in a struct that only contains a void pointer to the object.
