If I don't know C/C++, can I still create bindings for C/C++ libs?

Hello everybody!

I'm wondering whether I can create bindings for C/C++ libs if I don't know C/C++, after I've invested some time to learn about C/C++ FFI and the type system of C/C++.

At the moment I don't know unsafe Rust, however I'd be willing to finally learn that.

I'd also be willing to learn C (shouldn't take too long, right?) if this would make most sense, but I'm not interested in learning C++.

(Btw., If I should learn C: Should I do that before I learn unsafe Rust?)

Background:

I'd like to build a Chromium-based browser for power-users (a Rust-based UI around Chromium), so the bindings I'd create probably wouldn't be simple (maybe Chromium Embedded Framework would be enough, but I doubt it).

It's possible, and definitely easier for C. You will have to learn a bit of C along the way to be able to figure out how the C library manages memory and whether it has any thread safety in order to translate that to a Rust wrapper. bindgen will mostly handle struct layouts for you.

You may have to learn how to tinker with C build systems and package managers, but these are merely tedious, not difficult. Some knowledge of C ABI and use of Valgrind and debuggers may be needed when the code crashes.

With C++ it's much harder (if the library doesn't export a C interface). Libraries can have complex interfaces that use non-obvious features of C++. Interactions between C++ memory management and Rust's has a lot of hidden complexity. bindgen for C++ isn't nowhere near as complete, and tools like cxx are needed.

5 Likes

Thanks for your response, @kornel!

After reading your comment and then doing some research based on it, I discovered the following article by Google:

Supporting the Use of Rust in the Chromium Project

(It describes Googles efforts to enable the use of Rust code within Chromium).

From what they write there, it seems pretty difficult to write bindings for C++ code bases like Chromium even if you know C++...

So it's probably not realistically for me to think I would be able to create such bindings without properly learning C++...

Chromium Embedded Framework has an C FFI, so that is probably my best option.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.