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.
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.
It does not seem possible...
Information from here
"Direct interface to C++ from Ada is outside the scope of the Ada (at least 95)"
Green Hills Software published a great PDF about this as well.
http://www.ghs.com/download/whitepapers/ada_c++.pdf
I haven't touched Ada since 99/00 so my memory may be a bit sketchy but when we were working on a graphical windows app in Ada we needed to do some stuff in C++ (non mission critical) and the way we incorporated the two was to have the c++ stuff in a dll(s) and then create a C wrapper to that DLL and then use a pragma interface that specified the c wrapper and then we could call the methods within the dll from that interface.
I think it amounted to some triple maintenance because the method signatures had to be updated in the pragma interface and the C wrapper if they changed in the c++ dll.
Basically it was a pain. I think we had to use a Win32 Lean and Mean precomplier directive in the C wrapper.
Anyway, that is all the three of us who worked on the project can remember.
The problem with Ada to C++ is that C++ does NOT have a defined ABI.
Each compiler is allowed to define the most effecient ABI it can.
Thus interfacing from other languages (Ada) is a pain as you would need your Ada compiler to know which compiler the C++ was compiled with before it could generate the correct code to call any C++ method/function.
On the other hand the C ABI is well defined a standard across all compilers and as such provides a nice convenient interface for any language to connect with.
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);
You might be interested in this paper, which discusses an object-level binding of Ada to C++:
http://www.adacore.com/wp-content/uploads/2006/07/Class%5Flevel%5Finterfacing.pdf
Also, recent version of GNAT feature a powerful automatic binding generator.