Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Not to derail the OP too much, but I've also been C-curious for a while, but specifically for writing wrappers C++ projects (for FFI calls from rust, swift, etc.).

I've written a few such wrappers that work, but I have no idea what I'm doing WRT const void * et al. and would love to understand it a little better.

Are there any of these resources that would help me understand how to write better FFI code in C, while maybe skimping on details that are less important if I don't plan to write much code that is "C first"?

Thanks for any suggestions. Apologies in advance for wrong terminology and generally being a noob.



Your question is not clear; are you trying to call into C++ libraries from Rust/Swift using their FFI to C?

Something like this?

Rust/Swift -> <FFI> - > C -> C++ libraries

If so, you first need to understand ABIs and the C ABI in particular. Start here - https://stackoverflow.com/questions/2171177/what-is-an-appli...

Providing a C interface to C++ is done using "extern C" - https://stackoverflow.com/questions/1041866/what-is-the-effe...

So you should now have a binary C++ library but which exposes its public interfaces using C language conventions and linkage i.e. C ABI.

You can now call into the above using your Rust/Swift FFI. I am not familiar with these so you will have to look at their FFI docs. Here is one for Swift - https://stackoverflow.com/questions/24004732/how-to-call-c-f...

Be very careful about strictly following the rules for FFI since you are bridging two different languages with their own semantics and runtimes which could be very different from each other. See - https://en.wikipedia.org/wiki/Foreign_function_interface

The key is to understand what a ABI is and how the C ABI has become sort of de-facto standard for FFIs.


Thanks for your response! I would edit to clarify but missed the edit window on my post.

> are you trying to call into C++ libraries from Rust/Swift using their FFI to C?

Yes. Examples (just POC, getting things to link and compile): <https://github.com/n8henrie/brokencppmap> <https://github.com/n8henrie/swiftcpp>

Thanks for the links -- I've been putting things together here and there from a combination of SO and the Rust user forum, which is why I was wondering about a more formal reference focused on this use case.


You are on the right track; there is nothing difficult here but merely a bunch of moving parts to keep track of and understanding how they interact with each other. The SO articles i linked to earlier are very relevant and you should follow other links from their pages.

That said there is one other crucial point to keep in mind when calling C++ from C. Using "extern C" for function declarations informs the C++ compiler to emit code to C ABI conventions so it can be called from C code. But what about data structures which you might pass and use between C++-land and C-land? They have to follow strict "POD" criteria for compatible memory layouts. See for example - https://stackoverflow.com/questions/49407290/memory-layout-d...


Thanks again, I'll keep reading. For posterity, another good resource (at least for Swift): https://github.com/apple/swift/blob/main/docs/HowSwiftImport...




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: