Nurbol Alpysbayev
Nurbol Alpysbayev

Reputation: 21881

Can C++ and Rust programs compiled to wasm interoperate somehow?

Let's say I have one program written in Rust and another in C++. Given that they both are compiled to Wasm, can I somehow call a function in one program from the other?

Upvotes: 7

Views: 784

Answers (1)

Matthieu M.
Matthieu M.

Reputation: 299890

Yes, if they share the same ABI.

When compiling to assembly, what matters is the ABI, or Application Binary Interface:

  • How are types represented in memory?
  • How are arguments passed to a function?
  • ...

When you hear C is the Lingua Franca of programming languages, what it means is that any language that talks the C ABI1 can communicate with any other language that talks the C ABI.

Thus, whether targeting Windows on x64 or WebAssembly, what really matters is that both programs share the same convention (ABI) when talking to each other.

In your case, both Rust and C++ can talk C, so by communicating over a C API they can communicate on x86, x64, various ARM, ... and of course WASM.

1 As a convention, the owner of a platform defines the C ABI for the platform, and all C compilers implement the defined ABI when targeting this platform. This means there are multiple, incompatible, C ABIs; however since an ABI only matters when interacting at the binary level, which only happens when executed on the same platform, in practice there's a single C ABI of relevance in any given situation.

Upvotes: 10

Related Questions