News for hackers. Stuff that matters.
Re:CXX – safe interop between Rust and C++ by netr0ute on Thursday March 10, 2022 @10:10AM (#30627755)
What's the point of using C++ in Rust when you could use Rust instead and not have to add a cargo package?
Re:CXX – safe interop between Rust and C++ by tux3 on Thursday March 10, 2022 @10:12AM (#30627774)
It's not just about using C++ libraries from Rust, but also the other way around.

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.

Re:CXX – safe interop between Rust and C++ by codetrotter on Thursday March 10, 2022 @10:29AM (#30627970)
I have a pretty cool use case where I successfully interfaced between C++ and Rust code.

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

[0]: https://www.pjrc.com/store/octo28_adaptor.html

[1]: https://www.pjrc.com/teensy/td_libs_OctoWS2811.html

Re:CXX – safe interop between Rust and C++ by chris_overseas on Thursday March 10, 2022 @02:19PM (#30631213)
This sounds great, I'd been thinking about creating something similar (I also have a project with Teensy 3.2/4.0, OctoWS2811 and CLion). Is there any chance you'd be willing to open source it? In particular I'd be interested in what you did to get the Teensy code compiling and running on the host with the shims you mention.
Re:CXX – safe interop between Rust and C++ by girvo on Thursday March 10, 2022 @04:54PM (#30632978)
If your board supports it, the Zephyr RTOS and build tools support building your firmware for a Linux binary target too :) even has a neat RPC protocol to test the firmware on your computer rather than on the board.

Probably too much for a Teensy tho, but it is supported:

https://docs.zephyrproject.org/latest/boards/arm/teensy4/doc...

Re:CXX – safe interop between Rust and C++ by sgeisenh on Thursday March 10, 2022 @10:14AM (#30627806)
There is a lot of code written in C++. For a lot of large companies, one of the impediments to migrating from C++ to Rust is the investment required to expose the existing C++ interfaces in Rust. There are also many open source libraries written in C++ that you can more easily call from Rust using this bridge library.
Re:CXX – safe interop between Rust and C++ by detaro on Thursday March 10, 2022 @10:18AM (#30627846)
Rewriting a few million lines of existing C++ is not cheap nor quickly done.
Re:CXX – safe interop between Rust and C++ by deadcore on Thursday March 10, 2022 @10:13AM (#30627785)
If you have existing resources written in C++, external libs, maybe you've created a 'plugin' which is loaded by an application but the plugin is written in Rust and the app is written in C++ and it needs to invoke back into the main applications code. Just examples of stuff where I've had to interop Rust with other things outside of my control :)
Re:CXX – safe interop between Rust and C++ by mcronce on Thursday March 10, 2022 @10:18AM (#30627843)
It takes a lot less time than rewriting [component] in Rust
Re:CXX – safe interop between Rust and C++ by onceiwasthere on Thursday March 10, 2022 @10:14AM (#30627798)
I guess if you already have C++ that you'd prefer not to re-write. Also things like graphics programming, while making great strides in rust, are definitely still superior in C++ in certain aspects so you might want to write, say, an OpenGL interface in C++.
Re:CXX – safe interop between Rust and C++ by pjmlp on Thursday March 10, 2022 @10:17AM (#30627832)
Plenty of ecosystems where C++ rules and no one is going to rewrite any of those libraries.
Re:CXX – safe interop between Rust and C++ by ben-schaaf on Thursday March 10, 2022 @10:13AM (#30627790)
There's lots of useful C++ libraries that don't have (good) rust bindings.