Calling C/C++ from Motoko. Howto?

I want to reuse an existing C/C++ library that is too complex to be reimplemented in Motoko. I wonder what is the way to do that. I thought about creating a C++ canister that expose a query method for each C++ function and updates method for each class method that modifies the object state. Could you please indicate me how to proceed?

There is the experimental tool canpack which can do that for Rust, but I don’t know how hard it would be to get it running for C/C++

If I want to create an app that has the following requirements.

  1. 2 canisters where canister 1 is written in Motoko and canister 2 is written in C++
  2. I want canister 1 to be able to instantiate canister 2 and be able to call its methods

Would that be a possible solution? Do you see any problem with it or any way to improve it to achieve my goal?

If you’re talking separate canisters, there’s nothing special you need to do about it. Just make sure that they can serialize / deserialize each other’s messages. This is just like a Python backend server talking to a C++ backend server.

The difficulty is in putting C++ and Motoko code inside the same canister. But it would seem this is not what you’re looking for.

Yes, ideally I would want to to put motoko and C++ code inside the same canister. Is that possible?

Technically yes, but that’s way out of my area of expertise. Look into canpack (linked above). But unless it’s easy to do; or you really-really cannot do without having both Motoko and C++ code in the same canister; I would recommend having 2 canisters running side-by-side. Or just rewriting the smaller chunk of code in the other language.

2 Likes

This isn’t very useful information in the short term, but longer term, this will be possible with the wasm component model. @rvanasa ran an experiment a few months back where he successfully composed a wasm module with Rust and Motoko components

2 Likes

Is that the canpack project that @Severin mentioned above or is a different project? is there a repository to this “wasm component model”?

There was no code published because it was run as an internal experiment. It did show us that adoption of the wasm component model can give us access to capabilities like this though. We will adopt the wasm component model once it is more mature

1 Like

Hi @ildefons ,

The best way is to just create a C++ canister using icpp-pro, and create query or update endpoints that you then call from your Motoko canister.

The C++ canisters have full support of the Candid interface, and it will not be difficult to do this. I am more than happy to assist you with this task.

What library are you interested in using ?

1 Like

Hi @icpp , Thank you very much for your offer to help. And yes, icpp-pro seems to best option so far to solve my problem. I am interested in wrapping a C++ linear algebra library so I have access to matrix decomposition and matrix inverse functionalities. This would open a lot of possibilities to extend Motokolearn with plenty of clasic machine learning and optimizations methods. there are a few LA libraries and I am still trying to decide which one.

1 Like

tenor (3)

2 Likes

That concept of a wasm component model is very interesting.

How does it work? Are multiple wasm files that come from different languages combined into a single deployable wasm, so we can avoid the overhead of having to call another canister?

A great use case for that would also be the combination of a python wasm combined with a C/C++ wasm. That is so common in the engineering world, to code everything in python, but then optimize it with C/C++ when performance matters. Instead of doing it the traditional way to expose the C/C++ code via special bindings, perhaps it can be handled nicer using the wasm component model…???..

1 Like