Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you call C++ functions from Ada?

Tags:

c++

ada

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.

like image 281
paxos1977 Avatar asked Dec 08 '22 08:12

paxos1977


1 Answers

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);
like image 172
T.E.D. Avatar answered Dec 14 '22 23:12

T.E.D.