comp.lang.ada
 help / color / mirror / Atom feed
* calling an ada procedure from C++
@ 2003-11-12 15:11 Anthony Moss
  2003-11-17  4:04 ` red floyd
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: Anthony Moss @ 2003-11-12 15:11 UTC (permalink / raw)


I have an ada simulation and a windows C++ graphics program already created.
My task is to transfer the information from the ada written simulation to
the VisualC++ graphics program. This involves transfering a large structure
of information from ada to C++. To do this i have written a very small
program to pass a simple structure from ada to C++, but I am getting run
time access errors. I will place a copy of the code onto this message, so
could you tell me what is wrong and how to fix it.
thanks
Anthony Moss
The ada code is .....
package Output is

   type Record_T is
      record
         First    : Integer;
         Second   : Integer;
   end record;

   --type Record_Ptr is access Record_T;

   function Struct return Record_T;
   pragma Export (C, Struct, "Struct");

end Output;

package body Output is

   function Struct return Record_T is
      A_Record : Record_T :=(2,4);
      begin
         return A_Record;
      end Struct;

end Output;


The C++ code is..

typedef struct {
 int first;
 int second;
} A_Struct;

extern "C" {
 A_Struct Struct();
}

int main()
{
 A_Struct A = Struct();

 __asm nop;
 return 0;
}

ps when iinclude adainit() and adafinal I am getting
unresolved external symbol _adafinal.....
errors







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

* Re: calling an ada procedure from C++
  2003-11-12 15:11 calling an ada procedure from C++ Anthony Moss
@ 2003-11-17  4:04 ` red floyd
  2003-11-17  7:37 ` tmoran
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 10+ messages in thread
From: red floyd @ 2003-11-17  4:04 UTC (permalink / raw)


Anthony Moss wrote:
> I have an ada simulation and a windows C++ graphics program already created.
> My task is to transfer the information from the ada written simulation to
> the VisualC++ graphics program. This involves transfering a large structure
> of information from ada to C++. To do this i have written a very small
> program to pass a simple structure from ada to C++, but I am getting run
> time access errors. I will place a copy of the code onto this message, so
> could you tell me what is wrong and how to fix it.
> thanks
> Anthony Moss
> The ada code is .....
> package Output is
> 
>    type Record_T is
>       record
>          First    : Integer;
>          Second   : Integer;
>    end record;
> 
>    --type Record_Ptr is access Record_T;
> 
>    function Struct return Record_T;
>    pragma Export (C, Struct, "Struct");
> 
> end Output;
> 
> package body Output is
> 
>    function Struct return Record_T is
>       A_Record : Record_T :=(2,4);
>       begin
>          return A_Record;
>       end Struct;
> 
> end Output;
> 
> 
> The C++ code is..
> 
> typedef struct {
>  int first;
>  int second;
> } A_Struct;
> 
> extern "C" {
>  A_Struct Struct();
> }
> 
> int main()
> {
>  A_Struct A = Struct();
> 
>  __asm nop;
>  return 0;
> }
> 
> ps when iinclude adainit() and adafinal I am getting
> unresolved external symbol _adafinal.....
> errors
> 
> 
> 
> 

Cross language Ada is always tricky.  What platform are you using (both C++ and 
Ada compilers)?




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

* Re: calling an ada procedure from C++
  2003-11-12 15:11 calling an ada procedure from C++ Anthony Moss
  2003-11-17  4:04 ` red floyd
@ 2003-11-17  7:37 ` tmoran
  2003-11-17 10:38 ` Duncan Sands
  2003-11-17 21:13 ` sk
  3 siblings, 0 replies; 10+ messages in thread
From: tmoran @ 2003-11-17  7:37 UTC (permalink / raw)


>  --type Record_Ptr is access Record_T;
> ...
>  function Struct return Record_T is
>     A_Record : Record_T :=(2,4);
>     begin
>        return A_Record;
>     end Struct;
   Why do you have the Record_Ptr declaration commented out?  Did you try
returning a pointer, instead of a record, but it still didn't work?  How
about
   procedure Struct(Result : out Record_T) is
      A_Record : Record_T :=(2,4);
      begin
         Result := A_Record;
      end Struct;
so you are doing things C-style by passing a pointer parameter.



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

* Re: calling an ada procedure from C++
  2003-11-12 15:11 calling an ada procedure from C++ Anthony Moss
  2003-11-17  4:04 ` red floyd
  2003-11-17  7:37 ` tmoran
@ 2003-11-17 10:38 ` Duncan Sands
  2003-11-17 14:32   ` Hyman Rosen
  2003-11-17 21:13 ` sk
  3 siblings, 1 reply; 10+ messages in thread
From: Duncan Sands @ 2003-11-17 10:38 UTC (permalink / raw)
  To: Anthony Moss, comp.lang.ada

On Wednesday 12 November 2003 16:11, Anthony Moss wrote:
> I have an ada simulation and a windows C++ graphics program already
> created. My task is to transfer the information from the ada written
> simulation to the VisualC++ graphics program. This involves transfering a
> large structure of information from ada to C++. To do this i have written a
> very small program to pass a simple structure from ada to C++, but I am
> getting run time access errors. I will place a copy of the code onto this
> message, so could you tell me what is wrong and how to fix it.
> thanks

Don't return the value on the stack (i.e. via return), use an out parameter.
Standard C does not permit returning structures on the stack IIRC.

All the best,

Duncan.



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

* Re: calling an ada procedure from C++
  2003-11-17 10:38 ` Duncan Sands
@ 2003-11-17 14:32   ` Hyman Rosen
  2003-11-17 18:28     ` Frank J. Lhota
  0 siblings, 1 reply; 10+ messages in thread
From: Hyman Rosen @ 2003-11-17 14:32 UTC (permalink / raw)


Duncan Sands wrote:
> Standard C does not permit returning structures on the stack IIRC.

C has permitted this for some decades now.
But you must make sure that the C and Ada compilers
agree on how this is done.




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

* Re: calling an ada procedure from C++
  2003-11-17 14:32   ` Hyman Rosen
@ 2003-11-17 18:28     ` Frank J. Lhota
  2003-11-18  0:03       ` Ludovic Brenta
  0 siblings, 1 reply; 10+ messages in thread
From: Frank J. Lhota @ 2003-11-17 18:28 UTC (permalink / raw)



"Hyman Rosen" <hyrosen@mail.com> wrote in message
news:1069079554.9445@master.nyc.kbcfp.com...
> Duncan Sands wrote:
> > Standard C does not permit returning structures on the stack IIRC.
>
> C has permitted this for some decades now.
> But you must make sure that the C and Ada compilers
> agree on how this is done.

The original, vanilla K&R C book said:

"This implies that structures may not be assigned to or copied as a unit,
and that they can not be passed to or returned from functions. (These
restrictions will be removed in forthcoming versions.)"

IIRC said forthcoming versions were out there shortly afterwards. The
problem, as Hyman Rosen correctly pointed out, is the mechanism by which the
structure is returned. I know of at least mechanisms used to return a C
structure from a function:

1.    The function pushes return structure onto the stack, and the caller
pops the return value.
2.    The function has an additional, implicit parameter that specifies the
address of the return structure. The caller creates a buffer for this return
value, and passes a pointer to this buffer as the additional parameter. The
function then writes directly to this buffer.
3.    The function has an implicit static structure variable for the return
value. It fills in this static area with the return value, and returns the
address of this static area to the caller.

Hopefully, mechanism 3 is not being used by any current C compiler. This
method creates both reentrancy and multithreading problems. Some compilers
use either mechanism 1 or mechanism 2, depending on the nature of the
structure. Some use the rule of thumb that (1) is used if the structure is
small and (2) is used if the structure is large enough.

In order to be sure of how a particular C compiler returns a given structure
type, I would recommend doing an assembly listing for the current C code.
Also, be forewarned of the portability issue that different C compilers will
handle this in different ways.





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

* Re: calling an ada procedure from C++
  2003-11-12 15:11 calling an ada procedure from C++ Anthony Moss
                   ` (2 preceding siblings ...)
  2003-11-17 10:38 ` Duncan Sands
@ 2003-11-17 21:13 ` sk
  3 siblings, 0 replies; 10+ messages in thread
From: sk @ 2003-11-17 21:13 UTC (permalink / raw)
  To: comp.lang.ada

Anthony Moss <a.m@baesystems.com>:

 > ps when iinclude adainit() and adafinal I am getting
 > unresolved external symbol _adafinal.....
 > errors

Are you using GNAT ? (I am assuming Win32 platform ?)

You need to tell the linker about the Gnat library,
"libgnat" (-l option) on the command line.

-- 
-------------------------------------------------
-- Merge vertically for real address
--
--     s n p @ t . o
--      k i e k c c m
-------------------------------------------------




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

* Re: calling an ada procedure from C++
  2003-11-17 18:28     ` Frank J. Lhota
@ 2003-11-18  0:03       ` Ludovic Brenta
  2003-11-18  0:32         ` Frank J. Lhota
  0 siblings, 1 reply; 10+ messages in thread
From: Ludovic Brenta @ 2003-11-18  0:03 UTC (permalink / raw)



I think some compilers will also return small structures in processor
registers.  They will neither use the stack not any static memory, if
they can help it.

The basic point remains: how each compiler passes structures between
efunctions is compiler-specific.  It is however possible, both in C
and in Ada, to specify the exact bit pattern for a stucture, and thus
to pass the structure back and forth.  I think the most portable way
would be to pass pointers to the structures (as earlier suggested) but
if the OT uses GNAT (where the compiler handles both C and Ada), there
may be more efficient ways using representation clauses and Pragma
Convention.

BTW, C++ is a different beast altogether.  I would avoid it if
possible, especially if the C++ struct is really a class with methods
and parents or descendants.  Better stick with a strictly C struct.

My 2 cents.

-- 
Ludovic Brenta.



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

* Re: calling an ada procedure from C++
  2003-11-18  0:03       ` Ludovic Brenta
@ 2003-11-18  0:32         ` Frank J. Lhota
  2003-11-22  5:25           ` Dave Thompson
  0 siblings, 1 reply; 10+ messages in thread
From: Frank J. Lhota @ 2003-11-18  0:32 UTC (permalink / raw)



"Ludovic Brenta" <ludovic.brenta@insalien.org> wrote in message
news:m38ymezgo4.fsf@insalien.org...
>
> I think some compilers will also return small structures in processor
> registers.  They will neither use the stack not any static memory, if
> they can help it.

Yes, come to think of it, I have seen that for small structures, and I
should have included that in my list of mechanisms.

> The basic point remains: how each compiler passes structures between
> efunctions is compiler-specific.  It is however possible, both in C
> and in Ada, to specify the exact bit pattern for a stucture, and thus
> to pass the structure back and forth.

Even that is not necessarily true for all C compilers! The standard allows a
great deal of lattitude in the placement of bit field. According to the 89
standard (I'm not sure about C99), if you define

    struct Jello
    {
    unsigned Flavor : 5
    unsigned Servings : 3
    };

the compiler is free to implement this by placing Flavor in the high order
bits or in the low order bits. This creates a portability problem that has
resulted in the underuse of bit fields.

> I think the most portable way
> would be to pass pointers to the structures (as earlier suggested) but
> if the OT uses GNAT (where the compiler handles both C and Ada), there
> may be more efficient ways using representation clauses and Pragma
> Convention.
>
> BTW, C++ is a different beast altogether.  I would avoid it if
> possible, especially if the C++ struct is really a class with methods
> and parents or descendants.  Better stick with a strictly C struct.

The ACT people have provided support for interfacing to C++. In general, the
ACT and GNU people are to be congraduated for tackling the issue of
multi-language OO programming.

> My 2 cents.
>
> -- 
> Ludovic Brenta.





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

* Re: calling an ada procedure from C++
  2003-11-18  0:32         ` Frank J. Lhota
@ 2003-11-22  5:25           ` Dave Thompson
  0 siblings, 0 replies; 10+ messages in thread
From: Dave Thompson @ 2003-11-22  5:25 UTC (permalink / raw)


On Tue, 18 Nov 2003 00:32:23 GMT, "Frank J. Lhota"
<NOSPAM.lhota.adarose@verizon.net> wrote:

> 
> "Ludovic Brenta" <ludovic.brenta@insalien.org> wrote in message
> news:m38ymezgo4.fsf@insalien.org...
<snip>
> > The basic point remains: how each compiler passes structures between
> > efunctions is compiler-specific.  It is however possible, both in C
> > and in Ada, to specify the exact bit pattern for a stucture, and thus
> > to pass the structure back and forth.
> 
> Even that is not necessarily true for all C compilers! The standard allows a
> great deal of lattitude in the placement of bit field. According to the 89
> standard (I'm not sure about C99), if you define
> 
("latitude")

>     struct Jello
>     {
>     unsigned Flavor : 5
>     unsigned Servings : 3
>     };
> 
(need semicolon after each member-declaration)

> the compiler is free to implement this by placing Flavor in the high order
> bits or in the low order bits. This creates a portability problem that has
> resulted in the underuse of bit fields.
> 
Yes, unchanged in C99 6.7.2.1p10: "[t]he order of allocation of
bit-fields within [an allocation] unit" and "whether a bit-field that
[overflows] is put into the next unit or overlaps adjacent units" are
implementation-defined, which means they must be documented; and the
size of the allocation unit and (as a logical result) its alignment
are unspecified, which means they need not be documented.

<snip>
> The ACT people have provided support for interfacing to C++. In general, the
> ACT and GNU people are to be congraduated for tackling the issue of
> multi-language OO programming.
> 
("congratulated")

- David.Thompson1 at worldnet.att.net



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

end of thread, other threads:[~2003-11-22  5:25 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-11-12 15:11 calling an ada procedure from C++ Anthony Moss
2003-11-17  4:04 ` red floyd
2003-11-17  7:37 ` tmoran
2003-11-17 10:38 ` Duncan Sands
2003-11-17 14:32   ` Hyman Rosen
2003-11-17 18:28     ` Frank J. Lhota
2003-11-18  0:03       ` Ludovic Brenta
2003-11-18  0:32         ` Frank J. Lhota
2003-11-22  5:25           ` Dave Thompson
2003-11-17 21:13 ` sk

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