Any updates on calling C++ from Rust


#1

Hello Folks. So the one thing that blocks me from using Rust more is the difficulty with calling C++ libraries. Seems like FFI works fine for calls to C, but still cumbersome for C++. When I say cumbersome, I mean writing a C wrapper for the C++ code, which introduces a lot of boilerplate and maintenance cost. Are there any plans to improve the ability to call C++ from Rust? Seems like access to C++ libraries is probably something a lot of folks would want.


#2

D did it.

And apparently it’s a huge pain in the ass.

Rust doesn’t have many concepts that C++ does, and vice versa. For instance, C++ exceptions can mostly be caught in D, but that’s not a thing in Rust at all. Templates could kinda get mapped to Rust generics, except I’m under the impression that’d be extremely fragile for many reasons. The memory model of classes is also, I believe, a significant FFI problem. C++ inheritance vs Rust trait-based polymorphism is also a weird one.

I haven’t seen any meaningful description of a translation layer between Rust and C++, even in abstract. I’d certainly be curious to, though.

I doubt Rust/Qt is ever going to be a thing, which puts GNOME ahead of KDE on adopting us :grin:, but C++ linkage would surely be a nice feather in our cap.


#3

@myrrlyn thanks for the comment. Yeah, I understand what you mean. I do mostly scientific programming, so Boost and CGAL and all of those libraries are what I use most. I use Cython to interface python and C++ and it works pretty well. Everyone seems to say how hard it would be to implement this interface to C++, but Python, Julia, and so many other languages have succeeded at it. I certainly don’t have much knowledge of the lower level operations of the Rust–C++ bridge. As you say, the handling of exceptions, templates, etc., must be quite a challenge. That is why posted this thread, to see if there was perhaps any movement on this front since the last time I checked.


#4

You can look at https://github.com/servo/rust-bindgen , it partly support auto generation of rust binding to c++


#5

Looks like Cython skips the really hard stuff too.

The basics are probably pretty simple: C++ has namespaces and classes, we have modules and impl blocks, so fully qualified, non-generic, function names would likely be pretty simple to map back and forth (matching mangling schemes is probably a hair-puller though), so that should at least be doable. I honestly have no idea how I’d attempt tossing complex objects back and forth across FFI; at least C/Rust structs are reasonably isomorphic, but I don’t know how C++ classes and trait objects would match up.

I think the major abstract blocker is that full linkage is a truly Herculean effort, and partial linkage would always cause problems with the edge cases.

I hope that that’s a thing that starts attracting some thought under nightly, but I’m saying this as someone who will probably never contribute to that hypothetical effort so:/

It’s definitely a topic on people’s minds; I’ve seen the topic spring up here several times before.

I think step one would be making a C++/Rust function name translator to power an extern "C++" fn name declaration.


#6

COOL.

Makes sense that Servo has one.


#7

I think the project that has come the furthest with providing C++ bindings for Rust is this https://github.com/rust-qt/cpp_to_rust by @Riateche


#8

@emoon Thanks for the tip. I had not seen this library before. I will check it out.