comp.lang.ada
 help / color / mirror / Atom feed
* Interfacing ADA to C++ classes with parameters
@ 2002-04-03 12:46 John Haddon
  2002-04-03 13:53 ` Sergey Koshcheyev
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: John Haddon @ 2002-04-03 12:46 UTC (permalink / raw)




Dear All,

I'm having a problem writing an interface to a C++ class in which I want to
pass a parameter in addition to the object. I attach the package spec, the
main program and the C header file. The interface compiles and links with
the main program EXCEPT for TEST_INT_RET. On linkage, the following error is
given:

./ex6d_main.o: In function `ex6d_main__dispatch':
C:/PROGRA~1/GNAT/Examples_jfh/ex6d_main.adb:25: undefined reference to
`A::test_int_param(void)'

Why is this trying to refer to A::test_int_param(void) and not
A::test_int_param(integer,void)?

If the line calling test_int_param is omitted then the program links and
runs satisfactorily. I suspect that the problem is in the interface or
import function pragma parameters.

I am using Gnat 3.14p for Windows NT and GCC 2.95.3-5 for mingw as the C++
compiler. This examples is based upon EX6 in the GNAT documentation.

Any suggestions?

Many thanks in advance,


John

-----extract from souorce codes

package Ex6d_If is

   package A_Class is
      --
      --   Translation of C++ class A
      --

      type A is tagged
         record
            O_Value : Integer                   := 123;
            A_Value : Integer;                          -- := print(10);
            Vptr    : Interfaces.Cpp.Vtable_Ptr;
         end record;

      pragma Cpp_Class (Entity => A);
      pragma Cpp_Vtable (
         Entity      => A,
         Vtable_Ptr  => Vptr,
         Entry_Count => 7);

      --   Member Functions


      procedure Non_Virtual (This : in     A'Class );
      pragma Import (Cpp, Non_Virtual, "non_virtual__1A");
      pragma import_procedure (non_virtual,"non_virtual__1A",(A'class));


      function test_int_ret(This : in     A'Class)
      return integer;
      pragma Import (Cpp, test_int_ret, "test_int_ret__1A");

      function test_int_param(
            X    : in     integer;
            This : in     A'Class)
      return integer;
      pragma interface (Cpp, test_int_param);
-- IS THE FOLLOWING LINE CORRECT?
      pragma import_function(test_int_param,"
test_int_param__1A",(integer,A'Class),(integer));


      procedure Overridden (
            This : in     A );
      pragma Cpp_Virtual (Overridden,
         Vtable_Ptr  => Vptr,
         Entry_Count => 2);
      pragma Import (Cpp, Overridden, "overridden__1A");


      procedure Not_Overridden (
            This : in     A );
      pragma Cpp_Virtual (
         Entity      => Not_Overridden,
         Vtable_Ptr  => Vptr,
         Entry_Count => 4);

      pragma Import (Cpp, Not_Overridden, "not_overridden__1A");


      procedure My_Overridden (
            This : in     A );
      pragma Cpp_Virtual (
         Entity      => My_Overridden,
         Vtable_Ptr  => Vptr,
         Entry_Count => 6);--,      --  long form

      pragma Import (Cpp, My_Overridden, "my_overridden__1A");

      function Constructor return A'Class;
      pragma Cpp_Constructor (Entity => Constructor);
      pragma Import (Cpp, Constructor, "__1A");

   end A_Class;

end Ex6d_If;


--------------------------------
ex6d_main.adb ---------------------------------
with gnat.io;use gnat.io;
with ex6d_if;
procedure ex6d_main is

   use ex6d_if;
   use ex6d_if.A_Class;


   A_Obj : A_Class.A;

   procedure Dispatch (Obj : A_Class.A'Class) is
      x,y : integer := 98;
   begin
      put_line("In dispatch");
      put_line("calling non-virtual");
      Non_virtual (Obj);
      put_line("calling test_int_ret");
      x := test_int_ret(Obj);
      put("X=");put(x);new_line;
      put_line("calling test_int_param");
      x := test_int_param(y,Obj);
      put("X=");put(x);new_line;
      put_line("Calling overriden");
      Overridden (Obj);
      put_line("calling not_overridden");
      Not_Overridden (Obj);
      put_line("Calling my_overriden");
      my_Overridden (Obj);
   end Dispatch;

begin
   put("in ex6d_main");
   Dispatch (A_Obj);
end ex6d_main;



---- C Header file

class Origin {
 public:
  int o_value;
};

class A : public Origin {
 public:
  void    non_virtual (void);
  int     test_int_ret (void);
  int     test_int_param (int X);
  virtual void overridden (void);
  virtual void not_overridden (void);
  virtual void my_overridden (void);
  A();
  int   a_value;
};

________________________________________________________________


Dr. John F. Haddon, QinetiQ Fellow,

Centre for Robotics and Machine Vision,
X107 Building, Room 1032,
QinetiQ, Cody Technology Park, Farnborough, Hampshire, GU14 0LX, UK
Email:  jf_haddon@QinetiQ.com  Tel: 01252 395528; Fax: 01252 393942

Visiting Senior Fellow, CVSSP, University of Surrey.
Honorary Visiting Fellow, PANN, University of Exeter.
________________________________________________________________

The Information contained in this Email and any subsequent correspondence is
private and is intended solely for the intended recipient(s). For those
other than the recipient any disclosure, copying, distribution, or any
action taken or omitted to be taken in reliance on such information is
prohibited and may be unlawful.








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

* Re: Interfacing ADA to C++ classes with parameters
  2002-04-03 12:46 Interfacing ADA to C++ classes with parameters John Haddon
@ 2002-04-03 13:53 ` Sergey Koshcheyev
  2002-04-04  8:18   ` John Haddon
  2002-04-03 14:28 ` Mário Amado Alves
  2002-04-03 19:01 ` Stephen Leake
  2 siblings, 1 reply; 7+ messages in thread
From: Sergey Koshcheyev @ 2002-04-03 13:53 UTC (permalink / raw)


Hi,

The link name you use in pragma Import_Function below is wrong. It
corresponds to A::test_int_param(void) - try running c++filt and typing the
mangled name into it.

You have to figure out the correct link name and use that. Also, are you
sure that the A'Class argument should be the last in the argument list? I
would myself expect the "this" pointer to be the first argument, but I don't
know the low-level details.

Sergey.

"John Haddon" <jf_haddon@QinetiQ.com> wrote in message
news:a8ete9$sa8$1@hamble.qinetiq.com...
> Dear All,
>
> I'm having a problem writing an interface to a C++ class in which I want
to
> pass a parameter in addition to the object. I attach the package spec, the
> main program and the C header file. The interface compiles and links with
> the main program EXCEPT for TEST_INT_RET. On linkage, the following error
is
> given:
>
> ./ex6d_main.o: In function `ex6d_main__dispatch':
> C:/PROGRA~1/GNAT/Examples_jfh/ex6d_main.adb:25: undefined reference to
> `A::test_int_param(void)'
>
> Why is this trying to refer to A::test_int_param(void) and not
> A::test_int_param(integer,void)?

> <snip>
> -- IS THE FOLLOWING LINE CORRECT?
>       pragma import_function(test_int_param,"
> test_int_param__1A",(integer,A'Class),(integer));
> <snip>






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

* RE: Interfacing ADA to C++ classes with parameters
  2002-04-03 12:46 Interfacing ADA to C++ classes with parameters John Haddon
  2002-04-03 13:53 ` Sergey Koshcheyev
@ 2002-04-03 14:28 ` Mário Amado Alves
  2002-04-03 19:01 ` Stephen Leake
  2 siblings, 0 replies; 7+ messages in thread
From: Mário Amado Alves @ 2002-04-03 14:28 UTC (permalink / raw)


<<
-- IS THE FOLLOWING LINE CORRECT?
      pragma import_function(test_int_param,"
test_int_param__1A",(integer,A'Class),(integer));
>>

Isn't there a spurious space character on the left of the string
literal?




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

* Re: Interfacing ADA to C++ classes with parameters
  2002-04-03 12:46 Interfacing ADA to C++ classes with parameters John Haddon
  2002-04-03 13:53 ` Sergey Koshcheyev
  2002-04-03 14:28 ` Mário Amado Alves
@ 2002-04-03 19:01 ` Stephen Leake
  2 siblings, 0 replies; 7+ messages in thread
From: Stephen Leake @ 2002-04-03 19:01 UTC (permalink / raw)


"John Haddon" <jf_haddon@QinetiQ.com> writes:

> Dear All,
> 
> I'm having a problem writing an interface to a C++ class in which I want to
> pass a parameter in addition to the object. 

Interfacing directly to C++ is _not_ defined for Ada. You can make it
work, as you have discovered. However, a better approach is to export
functions from C++ using "extern C", and then import them to Ada using
"Import (C, ...)".

-- 
-- Stephe



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

* Re: Interfacing ADA to C++ classes with parameters
  2002-04-03 13:53 ` Sergey Koshcheyev
@ 2002-04-04  8:18   ` John Haddon
  2002-04-05 15:44     ` Robert Dewar
  0 siblings, 1 reply; 7+ messages in thread
From: John Haddon @ 2002-04-04  8:18 UTC (permalink / raw)


Thanks,

The mangled name was test_int_param__1Ai, data can now be passes into the
routine, modified and returned OK.  The reason that I'm trying to do this is
because I need to link to existing C++ code which is class based. Does
anyone know of an automatic tool for defining the binding between ADA and
C++,  C2ADA does not cope with a lot of C++ constructs.


John



"Sergey Koshcheyev" <serko84@hotmail.com> wrote in message
news:a8f1gd$eh8$1@ns.felk.cvut.cz...
> Hi,
>
> The link name you use in pragma Import_Function below is wrong. It
> corresponds to A::test_int_param(void) - try running c++filt and typing
the
> mangled name into it.
>
> You have to figure out the correct link name and use that. Also, are you
> sure that the A'Class argument should be the last in the argument list? I
> would myself expect the "this" pointer to be the first argument, but I
don't
> know the low-level details.
>
> Sergey.
>
> "John Haddon" <jf_haddon@QinetiQ.com> wrote in message
> news:a8ete9$sa8$1@hamble.qinetiq.com...
> > Dear All,
> >
> > I'm having a problem writing an interface to a C++ class in which I want
> to
> > pass a parameter in addition to the object. I attach the package spec,
the
> > main program and the C header file. The interface compiles and links
with
> > the main program EXCEPT for TEST_INT_RET. On linkage, the following
error
> is
> > given:
> >
> > ./ex6d_main.o: In function `ex6d_main__dispatch':
> > C:/PROGRA~1/GNAT/Examples_jfh/ex6d_main.adb:25: undefined reference to
> > `A::test_int_param(void)'
> >
> > Why is this trying to refer to A::test_int_param(void) and not
> > A::test_int_param(integer,void)?
>
> > <snip>
> > -- IS THE FOLLOWING LINE CORRECT?
> >       pragma import_function(test_int_param,"
> > test_int_param__1A",(integer,A'Class),(integer));
> > <snip>
>
>
>





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

* Re: Interfacing ADA to C++ classes with parameters
  2002-04-04  8:18   ` John Haddon
@ 2002-04-05 15:44     ` Robert Dewar
  2002-04-05 19:15       ` Ted Dennison
  0 siblings, 1 reply; 7+ messages in thread
From: Robert Dewar @ 2002-04-05 15:44 UTC (permalink / raw)


"John Haddon" <jf_haddon@QinetiQ.com> wrote in message news:<a8h246$1l5$1@hamble.qinetiq.com>...

> Does
> anyone know of an automatic tool for defining the binding 
> between ADA and C++,  C2ADA does not cope with a lot of 
> C++ constructs.

SGI has such a tool, but as far as I know they have never
released it (it is based on a proprietary C++ front end).



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

* Re: Interfacing ADA to C++ classes with parameters
  2002-04-05 15:44     ` Robert Dewar
@ 2002-04-05 19:15       ` Ted Dennison
  0 siblings, 0 replies; 7+ messages in thread
From: Ted Dennison @ 2002-04-05 19:15 UTC (permalink / raw)


dewar@gnat.com (Robert Dewar) wrote in message news:<5ee5b646.0204050744.58d9e842@posting.google.com>...
> "John Haddon" <jf_haddon@QinetiQ.com> wrote in message news:<a8h246$1l5$1@hamble.qinetiq.com>...
> 
> > anyone know of an automatic tool for defining the binding 
> > between ADA and C++,  C2ADA does not cope with a lot of 
> > C++ constructs.
> 
> SGI has such a tool, but as far as I know they have never
> released it (it is based on a proprietary C++ front end).

I think any such tool would *have* to be tied to a specific C++'s
front end, as there is no standard for how C++ compilers mangle
overloaded function names.


-- 
T.E.D.
Home     -  mailto:dennison@telepath.com (Yahoo: Ted_Dennison)
Homepage -  http://www.telepath.com/dennison/Ted/TED.html



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

end of thread, other threads:[~2002-04-05 19:15 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-04-03 12:46 Interfacing ADA to C++ classes with parameters John Haddon
2002-04-03 13:53 ` Sergey Koshcheyev
2002-04-04  8:18   ` John Haddon
2002-04-05 15:44     ` Robert Dewar
2002-04-05 19:15       ` Ted Dennison
2002-04-03 14:28 ` Mário Amado Alves
2002-04-03 19:01 ` Stephen Leake

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