views:

945

answers:

7

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.

+1  A: 

It does not seem possible...

Information from here

"Direct interface to C++ from Ada is outside the scope of the Ada (at least 95)"

Benoît
That just means that there is no *standard* way to do it, not that it is impossible.
T.E.D.
A: 

Green Hills Software published a great PDF about this as well.
http://www.ghs.com/download/whitepapers/ada_c++.pdf

ceretullis
Err...No. Green Hills does not use GCC. They have their own compiler which I believe uses the Itermetrics (now SofCheck?) front-end.However, much of the information in that PDF is actually compiler-agnostic. Ada specifies a surpising amount of this stuff in the standard.
T.E.D.
+1  A: 

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.

Bill Rawlinson
You only need to do that in Windows if you are using gcc to compile one side and VisualC++ to compile the other. Even gcc's C/C++ compiler would have this problem. The reason is that gcc and Microsoft use different linkers. If you use an Ada compiler that uses the Microsoft linker (like ObjectAda), you don't have this issue. If you stay off of Windows, you also don't have this issue. :-)
T.E.D.
+1  A: 

swig might be able to do it

iterationx
+4  A: 

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.

Martin York
Standard C, like most languages, does not define any ABI. The C ABI is pretty much "your CPU and OS's assembly ABI"; just on x86 you have at least four different procedure call conventions (stdcall, cdecl, thiscall, fastcall, ...)
crosstalk
Michael Ratanapintha: The C standard does define an ABI. It defines how parameters are paased to functions. Where the return address is stored and where the result of a fucntion call can be found. The same convention is used across all compilers.
Martin York
@Martin: Uh, what?
Robert P
@Robert: What! What?
Martin York
+2  A: 

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);
T.E.D.
+1  A: 

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.

Matt