CXX – safe interop between Rust and C++
cxx.rs
cxx.rs
CXX-Qt: Safe Rust bindings for Qt - https://news.ycombinator.com/item?id=30525752 - March 2022 (108 comments)
CXX - Safe interop between Rust and C++ - https://news.ycombinator.com/item?id=26565444 - March 2021 (32 comments)
CXX – safe interop between Rust and C++ - https://news.ycombinator.com/item?id=25126280 - Nov 2020 (1 comment)
The CXX Debate - https://news.ycombinator.com/item?id=24245372 - Aug 2020 (4 comments)
https://news.ycombinator.com/item?id=27726982 (July 2021)
https://news.ycombinator.com/item?id=27284079 (May 2021)
https://news.ycombinator.com/item?id=27236708 (May 2021)
https://news.ycombinator.com/item?id=26886074 (April 2021)
https://news.ycombinator.com/item?id=26245003 (Feb 2021)
https://news.ycombinator.com/item?id=26158300 (Feb 2021)
(I made that list using the same software :))
Worth noting also is autocxx [1], which is like bindgen, but generates cxx directives. Very clever too. It even allows owning C++ types on the stack.
extern (C++) int foo(long x);
and takes care of the C++ name mangling and function call ABI for your. It works for structs, inheritance, COM classes, member functions, namespaces, typedefs, even templates.[1] https://github.com/rust-lang/rust/issues/5853
[2] https://github.com/rust-lang/rfcs/issues/602
[3] https://internals.rust-lang.org/t/better-c-interoperability/...
[4] https://rust-lang.github.io/rfcs/2873-inline-asm.html#implem...
You're conflating DMD (the reference compiler) with the language here. LDC and GDC (the LLVM and GCC based D compilers) both support GCC style "extended" inline assembly[0] and LDC even allows you to write inline LLVM IR.
Each of the D compilers supports inline assembler for each of the targets the particular compiler supports.
GDC used to support it (long story but that code is now the LDC impl), but it was removed a long time ago. GDC isn't that much younger than dmd.
To improve interactivity with C, we even went so far as to build a C compiler into the D compiler. It was officially released this week.
But things like `struct S *` with no definition of `S` are handled properly.
He's like the Greek god of proc macros. I don't know if he was involved at all in their creation, but he's by far the most prolific proc macro author, and he seems to have an innate understanding of how to use them that goes far beyond what I imagine anyone really considered when they were created. Rust would still be the same language without his work, but it wouldn't feel at all like it, and there's a strong case to be made that Rust would not be anywhere close to as successful as it is today with him.
As someone who has been dragged down to the proc macro layer somewhat against my will, I'm not sure if I should be in awe or terror of an intellect that can be so consistently successful and useful in that world.
It still would be painful to maintain and update your Rust project and cargo configs with your C++ code alongside a third party crate maintained by one person in the project. D Lang is probably the only language that has this built into into the language and is done automatically.
I'd still rather go for a first party library with this support or C++ interop support feature built into the language like D with the whole requirement of it being completely automatic.
https://forums.swift.org/t/swift-and-c-interoperability-work...
https://github.com/apple/swift/blob/main/docs/CppInteroperab...
https://github.com/apple/swift/blob/main/docs/CppInteroperab...
It is very actively developed though so I trust that we'll get there eventually.
Which is where CXX comes in: the static analysis tells you if you're holding it wrong. So to your first point, the compiler will tell you exactly what needs to change.
I don't know what the developer experience with D/C++ interop looks like, but unless I'm missing something big I'd assume you'd still need to make changes when the library has updates.
https://crates.io/crates/crust
Seems to be an actually useful P2P networking library, not name squatted.
Most C integration recommendations I've seen suggest using bindgen, but that doesn't give safe bindings.
Some of these properties could be deduced by a Sufficiently Smart Compiler, but you'd probably need an A.I. that reads the docs.
That's also true, in general, for C++, but CXX is nevertheless attempting to solve that problem.
CXX requires you to give some extra information when declaring the bridge. Presumably that can allow things to be less automatic.
I'm guessing that the reality is that if your C++ code is relatively idiomatic then CXX will work with it, but stray too far from idiomatic C++ - i.e. towards a C-style interface and CXX won't be able to do such a good job.
But it would be good to hear something more definitive.
You have 15 million lines of C++ code, and you want to start writing some components in Rust. At the interface, you need a bridge.
This was for a project consisting of a sculpture with a bunch of LED strips on it, and my part was to do the programming, electronics and wiring for it.
To control the LED strips I use Teensy 3.2 microcontroller boards, with the OctoWS2811 adaptor board [0], along with an industrial singleboard computer.
The Teensy 3.2 boards I programmed using the Arduino libraries and the OctoWS2811 library [1]. The code I wrote for this part was C++. (But I don’t use the Arduino IDE, I use JetBrains CLion.)
Compiling and flashing the firmware for the Teensy, and disconnecting and reconnecting cables etc was making the development a bit annoying.
So in Rust I wrote a LED simulator program that renders the “pixels” of the LED strips on my screen, and I compiled my C++ code that I’ve written for the microcontrollers on the host, with some shims that I wrote to provide some functions that my firmware uses, and I link this with my Rust program.
The end result is that I have a fully functioning LED simulator that I use while developing, that runs on my computer. It runs on both macOS and Linux. And then the same C++ code of mine gets included and built without modification when I build my firmware for the microcontrollers.
It is work that I am very satisfied about :)
Should also note that I use the bindgen crate, not cxx. https://crates.io/crates/bindgen
Probably too much for a Teensy tho, but it is supported:
https://docs.zephyrproject.org/latest/boards/arm/teensy4/doc...