* Question: Exporting ADA functions to C
@ 2001-03-21 16:16 Tilman Gloetzner
2001-03-21 18:58 ` Ted Dennison
0 siblings, 1 reply; 3+ messages in thread
From: Tilman Gloetzner @ 2001-03-21 16:16 UTC (permalink / raw)
Hello,
I am trying to hand over a c-struct to an ada record and vice versa. The
gnat RM says that there are
2 ways to do it:
Ada RM (page 215):
Interfacing to C
Interfacing to C with GNAT can use one of two approaches:
1. The types in the package Interfaces.C may be used.
2. Standard Ada types may be used directly. This may be less
portable
to other compilers, but will work on all GNAT compilers, which
guarantee correspondence between the C and Ada types.
...
* Ada arrays map directly to C arrays.
* Ada records map directly to C structures.
...
I choose the 1st way because the RM suggests that the code is better
protable. Unfortunatelly, my test progam crashes with a core dump. Not
that I am surprised, but what did I do wrong ? First, I suspected that I
better hand over the string as pointer, so I commented it(now just
handing over an Integer and a Float). The program still crashed.
Thank you,
Tilman
======================= Test Program
====================================
with INTERFACES.C;use INTERFACES.C;
with INTERFACES.C.STRINGS; use INTERFACES.C.STRINGS;
use INTERFACES;
package Ada_2_C is
type Rec is
record
FLOAT_FIELD: FLOAT;
STR_FIELD: STRING(1..16);
INT_FIELD: INTEGER;
end record;
type Rec_C is
record
FLOAT_FIELD: C_Float;
STR_FIELD: Char_Array(1..16);
INT_FIELD: Int;
end record;
pragma Convention (C,Rec_C);
procedure Register_Record(C_Structure: in Rec_C);
procedure Get_Record(C_Structure: out Rec_C);
pragma Export(C,Register_Record,"C_Register_Rec");
pragma Export(C,Get_Record,"C_Get_Rec");
end Ada_2_C;
package body Ada_2_C is
Temp: Rec ;
procedure Register_Record(C_Structure: in Rec_C) is
begin
Temp.FLOAT_FIELD := FLOAT(C_Structure.Float_FIELD)+1.0;
Temp.Str_Field := To_Ada(C_Structure.Str_Field);
Temp.Str_Field(2) := 'B';
Temp.Int_Field :=INTEGER(C_Structure.Int_Field) + 1;
end Register_Record;
procedure Get_Record(C_Structure: out Rec_C) is
begin
C_Structure.FLOAT_FIELD := C_FLOAT(Temp.FLOAT_FIELD);
C_Structure.STR_FIELD := To_C(Temp.STR_FIELD);
C_Structure.Int_Field := Int(Temp.Int_Field);
end Get_Record;
end Ada_2_C;
// C program containing "main"
#include<stdio.h>
struct Ada_rec {
float Float_Field;
char Str_Field[16];
int Int_Field;
};
extern void adainit (void);
extern void adafinal (void);
extern void C_Register_Rec(struct Ada_rec Structure);
extern void C_Get_Rec(struct Ada_rec Structure);
main(int argc,char **argv) {
struct Ada_rec Structure = {0.0,"TEST TEST TEST T",0};
printf("Start main\n");
adainit();
printf("pushing struct from c to ada...\n");
C_Register_Rec(Structure);
printf("pulling struct (record) from ADA \n");
C_Get_Rec(Structure);
printf("Float = %f\nStr = \nInt = %d",
Structure.Float_Field,
Structure.Str_Field,
Structure.Int_Field);
adafinal();
printf("End main\n");
}
# Compilation
gnatmake -c ada_2_c.adb -o ada_2_c.o
gcc -c ada_2_c.adb
gnatbind -n ada_2_c
gnatlink ada_2_c.ali c_main.o -o ada_n_c
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Question: Exporting ADA functions to C
2001-03-21 16:16 Question: Exporting ADA functions to C Tilman Gloetzner
@ 2001-03-21 18:58 ` Ted Dennison
2001-03-21 19:35 ` Robert A Duff
0 siblings, 1 reply; 3+ messages in thread
From: Ted Dennison @ 2001-03-21 18:58 UTC (permalink / raw)
In article <3AB8D3F9.FC6C6B9E@uundz.de>, Tilman Gloetzner says...
>I am trying to hand over a c-struct to an ada record and vice versa. The
..
>protable. Unfortunatelly, my test progam crashes with a core dump. Not
..
> procedure Register_Record(C_Structure: in Rec_C);
> procedure Get_Record(C_Structure: out Rec_C);
>
> pragma Export(C,Register_Record,"C_Register_Rec");
> pragma Export(C,Get_Record,"C_Get_Rec");
..
>extern void adainit (void);
>extern void adafinal (void);
>extern void C_Register_Rec(struct Ada_rec Structure);
>extern void C_Get_Rec(struct Ada_rec Structure);
Ada cannot deal with C "struct" parameters. Pointer to structs are ok, but not
structs themselves. If I remember correctly, this is because C itself has no
real standard (or no universally-followed standard) on how structs are passed,
but that could be wrong.
Anyway, the proper C prototypes for your exported Ada routines would be:
extern void C_Register_Rec (struct Ada_rec * Structure);
extern void C_Get_Reg (struct Ada_rec * Structure);
---
T.E.D. homepage - http://www.telepath.com/dennison/Ted/TED.html
home email - mailto:dennison@telepath.com
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Question: Exporting ADA functions to C
2001-03-21 18:58 ` Ted Dennison
@ 2001-03-21 19:35 ` Robert A Duff
0 siblings, 0 replies; 3+ messages in thread
From: Robert A Duff @ 2001-03-21 19:35 UTC (permalink / raw)
Ted Dennison<dennison@telepath.com> writes:
> In article <3AB8D3F9.FC6C6B9E@uundz.de>, Tilman Gloetzner says...
> >I am trying to hand over a c-struct to an ada record and vice versa. The
You might want to look at pragma C_Pass_By_Copy.
- Bob
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2001-03-21 19:35 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-03-21 16:16 Question: Exporting ADA functions to C Tilman Gloetzner
2001-03-21 18:58 ` Ted Dennison
2001-03-21 19:35 ` Robert A Duff
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox