# Develop "Julian" API for a C++ library?

**URL:** https://discourse.julialang.org/t/develop-julian-api-for-a-c-library/101127
**Category:** General Usage
**Created:** [July 3, 2023, 2:07pm UTC](https://discourse.julialang.org/t/develop-julian-api-for-a-c-library/101127 "2023-07-03T14:07:06Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)
#### Post date: [July 3, 2023, 2:07pm UTC](https://discourse.julialang.org/t/develop-julian-api-for-a-c-library/101127/1 "2023-07-03T14:07:06Z")

</div>

I have an vendor-produced C++ library that I would like to write a Julia wrapper for (I only have headers and shared objects for it). The typical use case is that the library is called from a user-created file that just has a main method (basically, a simulation script: initialize things, and loop over steps setting simulation state to suit the creator of the script). I realize that there might be _good_ ways of doing this, but probably not one single _best_ way of doing this.

What I am really looking for is a link to some discussion of how other people did this, perhaps projects on Github that I can peruse? The existing C++ code is typically called like this:

```plaintext
auto w = Widget();
w->init();
for (int i = 0; i < N; ++i) {
    Foo *a = w.get_foo();
    a->prop1 = 1;
    a->prop2 = 2;
    a->method("bar");

    w->frob();
}

```

I was thinking that the look of a Julia wrapper might be:

```julia
w = Widget()
init(w)

for i ∈ 1:N
    a = get_foo(w)
    a.prop1 = 1
    a.prop2 = 2
    method(a, "bar")

    frob(w)
end

```

---

<div class="post-metadata">

### Author: ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)
#### Post date: [July 3, 2023, 11:33pm UTC](https://discourse.julialang.org/t/develop-julian-api-for-a-c-library/101127/2 "2023-07-03T23:33:02Z")

</div>

Did you manage to take a look at the [JuliaInterop/CxxWrap.jl: Package to make C++ libraries available in Julia](https://github.com/JuliaInterop/CxxWrap.jl) package?

Also - you might find the `issues` section instructive for additional challenges people faced.

Disclaimer: I have no practical experience with calling C/C++ from Julia. I just did some reading on the subject out of curiosity.

---

<div class="post-metadata">

### Author: ![Jake](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jake/32/46007_2.png) [@Jake](https://discourse.julialang.org/u/Jake)
#### Post date: [July 4, 2023, 12:58am UTC](https://discourse.julialang.org/t/develop-julian-api-for-a-c-library/101127/3 "2023-07-04T00:58:09Z")

</div>

I have a couple of dynamic libraries that I needed to access. I have used ccall statements. I have started to use [Clang](https://github.com/JuliaInterop/Clang.jl) to generate the wrapper code. This gives a [good start](https://discourse.julialang.org/t/clang-edit-requirements/101031) at developing the code I require.

I one case I created the ccall statements by hand and then when I learned about Clang I tried it. In the simpler calls it created the code I wanted. In other cases it was a really good start, eliminating much tedious work.

---

<div class="post-metadata">

### Author: ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)
#### Post date: [July 5, 2023, 12:37pm UTC](https://discourse.julialang.org/t/develop-julian-api-for-a-c-library/101127/4 "2023-07-05T12:37:32Z")

</div>

Thanks for the suggestions, I hadn’t thought of looking in “issues,” but that was interesting as well. I had also looked at [PyCall.jl](https://github.com/JuliaPy/PyCall.jl) and [PythonCall.jl](https://github.com/cjdoris/PythonCall.jl) for some ideas.

The real issue is that the API is very non-Julian on the surface. I’m new to Julia, and frankly have been amazed at how well designed the APIs are in Julia. I have been used to seeing OO APIs designed similarly to the one that I am trying to wrap now: You have a tree-like relationship with some sort of manager object (simulation object in my case). The simulation has things that it manages that you can get at with `.` or `->`, and sometimes those things have sub-objects that they manage.

So, I have collections of ~~methods and parameters~~ fields and methods in an object, and some of the objects may themselves contain methods and objects as well.

I have been trying to use overloads of `Base.getproperty` to handle getting the properties, but I wasn’t sure how to use `Base.getproperty` (or somethings similar) to:

```julia
# may not even be possible:
main_wrapped_object.method()
main_wrapped_object.subobject.method()

```

… or if that was even a good idea. I wasn’t expecting this to be easy, and I realized that I may end up having to radically alter the look of the API on the Julia side. Right now for methods and functions that ends up being changing API calls like `animal.eat(food)` to `feed(animal, food)` (i.e., change the verb).

Maybe I could incorporate this [trick from SO](https://stackoverflow.com/questions/39133424/how-to-create-a-single-dispatch-object-oriented-class-in-julia-that-behaves-l/39150509#39150509)?
