Can you call C++ functions from Ada?
I'm wondering if there is a way to do this directly, without doing the implementation in C and writing a C++ wrapper & and Ada wrapper, e.g. I would like to go c++ -> Ada rather than c++ -> c -> Ada.
The only really compiler-agnostic answer I can give you is that it is just as possible as calling C++ from C on your system.
Much like with C, you have to figure out your C++ routine's name-mangled symbol and write a binding on the C (in this case the Ada) side that links to that mangled name. You will also probably have to do some things on the C++ side, like declaring the C++ function extern.
If you can declare your C++ function extern "C", it's easy. Just do that on the C++ side, and use Ada's standard C import features on the Ada side.
Example:
in your cpp:
extern "C" int cpp_func (int p1, int p2) {
; // Whatever..
}
in your .adb:
function cpp_func (p1, p2 : Interfaces.C.Int) return Interfaces.C.Int;
pragma Import (C, cpp_func);
...
Result : constant Interfaces.C.Int := cpp_func (1, 2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With