comp.lang.ada
 help / color / mirror / Atom feed
* Ada/C++ linkange on SGI.
@ 1996-10-08  0:00 Dennis LaCroix
  1996-10-09  0:00 ` Kenneth Almquist
  0 siblings, 1 reply; 2+ messages in thread
From: Dennis LaCroix @ 1996-10-08  0:00 UTC (permalink / raw)



Does anyone have any experience with trying to call C++ class functions
from an Ada program?  I'm trying to create instances of a class in
C++, and call functions for particular instances from Ada.

I'm on an SGI with the following configuration:

Operating system: IRIX 6.2
C++ compiler    : MIPSpro C++ 6.2
Ada compiler    : Verdix VADS 6.2

I've managed to call standalone C++ functions from Ada.  Here is an
example of what I did:

First compile the C++ code and make it into a shared object file using
a command similar to the following:

CC -o cpp_routine.so -elf -shared cpp_routine.c++

Then create an Ada wrapper routine to be used throughout the Ada code:

--------
with System;
package Wrapper is

   procedure CPP_Routine;
   pragma Interface (C, CPP_Routine);
   pragma Interface_Name (CPP_Routine, "cpp_routine__Fv");

end Wrapper
--------

I used the "nm" command on the cpp_routine.so file to get the link name
used in the Interface_Name pragma.  I'm currently experimenting with
the 'extern "C"' construct in C++ to avoid having to appending the
suffix (the format of the suffix is documented in "C++ Language System
Overview - Chapter 9, Type-Safe Linkage for C++" - type "insight" on
the SGI to bring up this online documentation). 

I used an Ada link command similar to the following to link the
executable:

a.ld -L lib -o test_c++.exe ada_main -old_ld cpp_routine.so -rpath 
{directory with cpp_routine.so in it}

This technique also works with functions that have parameters.

Here's what I CAN'T get to work:

I've got a C++ class with various public functions and variables.  I've
created a shared object file (.so) for the class.  If I use the "nm" 
command on this file then I see link names for each of the class
functions. 

I've also got a C++ file that declares an instance of this class.  I've
created another shared object file containing the instance.  When I do
an "nm" on this file then all I see is a link name for the object
instance.

I can use the Ada wrapper technique given above to call the functions
in the CLASS object file (but only for simple functions like
'cout << "yup, worked" << endl', parameter passing and accessing class
data doesn't work.)

If, for example, I had two instances of the class I want to be able
to call INSTANCE1.class_function() and INSTANCE2.class_function() from
Ada.  Does anyone have any insight about how I might do this?

Thanks.




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Ada/C++ linkange on SGI.
  1996-10-08  0:00 Ada/C++ linkange on SGI Dennis LaCroix
@ 1996-10-09  0:00 ` Kenneth Almquist
  0 siblings, 0 replies; 2+ messages in thread
From: Kenneth Almquist @ 1996-10-09  0:00 UTC (permalink / raw)



> C++ compiler    : MIPSpro C++ 6.2
> Ada compiler    : Verdix VADS 6.2

I assume that's an Ada 83 compiler.

> If, for example, I had two instances of the class I want to be able
> to call INSTANCE1.class_function() and INSTANCE2.class_function() from
> Ada.  Does anyone have any insight about how I might do this?

Well, first you want to write an Ada definition which corresponds to
the C++ class.  If you have it, you should read the compiler documen-
tation to determine how each compiler does layout.  Otherwise, you can
probably figure it out by trial and error.  Typically, you will end up
with something like:

	class cc {			type cc is record
		int a;			    a : integer;
		int b;			    b : integer;
		void operate();		end record;
	};

If the C++ class contains any virtual members, the C++ compiler will
add a tag field which identifies the type, so your Ada declaration
might look like:

	type cc is record
	    tag : system.address;
	    a : integer;
	    b : integer;
	end record;

Then if you create any class instances using Ada, you have to
initialize the tag field by copying it from a class instance created
using C++.

Calling a non-virtual member function from Ada is no different from
calling any other C++ function from Ada, except that in the C++ code
one argument will precede the function name.

	C++:  a.operate();
	Ada:  operate(a);

Virtual functions in C++ support run time dispatching, which is not
available in Ada 83.  (It is in Ada 95.)  If you know the type at
compile time, virtual functions require no special handling.  Otherwise,
your best bet in Ada 83 is to write wrapper function in C++ which calls
the virtual function.  For example:

	void
	operate(class cc *a) {
		a->operate();
	}

The above approaches should work with most compilers, but again, if
you have access to documentation describing your compilers, your best
bet is to work from that.
				Kenneth Almquist




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1996-10-09  0:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-10-08  0:00 Ada/C++ linkange on SGI Dennis LaCroix
1996-10-09  0:00 ` Kenneth Almquist

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox