# Simplest way to wrap virtual c++ class

**URL:** <https://discourse.julialang.org/t/simplest-way-to-wrap-virtual-c-class/4977>\
**Category:** New to Julia\
**Tags:** ccall, cxx\
**Created:** [July 20, 2017, 1:44pm UTC](https://discourse.julialang.org/t/simplest-way-to-wrap-virtual-c-class/4977 "2017-07-20T13:44:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [July 20, 2017, 1:44pm UTC](https://discourse.julialang.org/t/simplest-way-to-wrap-virtual-c-class/4977/1 "2017-07-20T13:44:42Z")

</div>

Hi,

I would like to wrap a C++ library that works with virtual methods. If I understand well, the problem boils down to calling the class `Base` for the reduced library below (so we have a simple working example) from Julia. Note that I made a new class `B` so I can provide a function `history_f h` from Julia.

Ultimately, `history_f h` will be the right hand side of a differential equation. Hence, it is likely to be called many times.

I would appreciate if you can provide any advice about wthethger to use Cxx.jl or CxxWrap.jl to do this. What will be the simplest / most efficient?

Thank you for your help,

Best regards

```nohighlight
#include <iostream>

typedef double (*history_f) (double); 
double fex(double x){return -1.;};

class Base
{
    virtual double history(double) = 0;
    public:
    void solve(){
        for (int i=0;i<3;i++){
            std::cout<<history((double) i)<<" \n";
            }
    }
} ;

class A:public Base
{
    double history(double x){return x;}   
};

class B:public Base
{
    public:
    B(history_f h){f=h;}
    double history(double x){return f(x);}

    history_f f;    
};

int main()
{
    A a;
    a.solve();
    std::cout<<"ok\n";
    B b(fex);
    b.solve();
    std::cout<<"ok\n";
}

```

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [July 20, 2017, 3:12pm UTC](https://discourse.julialang.org/t/simplest-way-to-wrap-virtual-c-class/4977/2 "2017-07-20T15:12:49Z")

</div>

For a small number of methods – on the order of, say, under a few 10s of signatures – the simplest way is almost always to create an `extern C { ... }` wrapper, which can be done with no knowledge of or dependency on `julia.h`.

As far as Cxx and CxxWrap:

- Cxx operates at LLVM IR level and can potentially do aggressive cross-language inlining, which _could_ give some performance edge for very hot code.
- the advantage of both (in somewhat different ways) is they have a lot of logic built-in already for working with, e.g., Julia arrays. This can save time if your interfaces are complicated.

The disadvantage of Cxx is complex build and distribution issues; CxxWrap allows localization of complexity to the developer side because binding shared libraries can be precompiled (as can the dependent Julia wrappers, which is helpful for end-user experience).

---

<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:** [July 20, 2017, 3:38pm UTC](https://discourse.julialang.org/t/simplest-way-to-wrap-virtual-c-class/4977/3 "2017-07-20T15:38:33Z")

</div>

Since this is an interesting example, I added it to the CxxWrap [inheritance example](https://github.com/JuliaInterop/CxxWrap.jl/blob/master/deps/src/jlcxx/examples/inheritance.cpp#L117)

This can be used from Julia like this, assuming the module is called `VirtualSolver`:

```julia
a = VirtualSolver.A()
VirtualSolver.solve(a)

b = VirtualSolver.B(safe_cfunction(x -> 2x, Float64, (Float64,)))
VirtualSolver.solve(b)

```

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [July 20, 2017, 5:54pm UTC](https://discourse.julialang.org/t/simplest-way-to-wrap-virtual-c-class/4977/4 "2017-07-20T17:54:21Z")

</div>

Wow thank you for your help! Trying your wrapper, I did

```bash
clang++ -shared -std=c++11 -undefined dynamic_lookup -I /Users/rveltz/.julia/v0.6/CxxWrap/deps/src/jlcxx/include -I /Applications/Julia-0.6.app/Contents/Resources/julia/include/julia/ -L/Users/rveltz/.julia/v0.6/CxxWrap/deps/usr/lib/ -L/Applications/Julia-0.6.app/Contents/Resources/julia/lib/ -L/Applications/Julia-0.6.app/Contents/Resources/julia/lib/julia/ -o libVirtualSolver.dylib wrap2.cpp

```

and then

```julia
using CxxWrap
wrap_modules(joinpath(pwd(),"libVirtualSolver"))

```

and I got

```julia
ERROR: could not load library "/Users/rveltz/work/prog_gd/julia/DDEArndt.jl/libVirtualSolver"
dlopen(/Users/rveltz/work/prog_gd/julia/DDEArndt.jl/libVirtualSolver.dylib, 9): Symbol not found: _jl_tls_states
  Referenced from: /Users/rveltz/work/prog_gd/julia/DDEArndt.jl/libVirtualSolver.dylib
  Expected in: flat namespace
 in /Users/rveltz/work/prog_gd/julia/DDEArndt.jl/libVirtualSolver.dylib
Stacktrace:
 [1] dlopen(::String, ::UInt32) at ./libdl.jl:97
 [2] load_modules at /Users/rveltz/.julia/v0.6/CxxWrap/src/CxxWrap.jl:139 [inlined]
 [3] wrap_modules at /Users/rveltz/.julia/v0.6/CxxWrap/src/CxxWrap.jl:359 [inlined] (repeats 2 times)

```

Can you give me one push please?

---

<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:** [July 20, 2017, 6:26pm UTC](https://discourse.julialang.org/t/simplest-way-to-wrap-virtual-c-class/4977/5 "2017-07-20T18:26:25Z")

</div>

I think you need to add `-DJULIA_ENABLE_THREADING` to the compile flags.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 20, 2017, 6:50pm UTC](https://discourse.julialang.org/t/simplest-way-to-wrap-virtual-c-class/4977/6 "2017-07-20T18:50:19Z")

</div>

What DDE solver are you wrapping?

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [July 20, 2017, 8:17pm UTC](https://discourse.julialang.org/t/simplest-way-to-wrap-virtual-c-class/4977/7 "2017-07-20T20:17:24Z")

</div>

I will give a try to [this one](https://web.archive.org/web/20050209123244/http://www.iam.uni-bonn.de/~arndt/programme.html). I used it in the past and it was very good.
