From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,55737cdd9f66c8ee,start X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-03-21 08:26:53 PST Path: supernews.google.com!sn-xit-02!supernews.com!news.gv.tsc.tdk.com!falcon.america.net!sunqbc.risq.qc.ca!newsfeed.mathworks.com!ptdnetP!newsgate.ptd.net!newsfeed00.sul.t-online.de!newsmm00.sul.t-online.com!t-online.de!news.t-online.com!not-for-mail From: Tilman Gloetzner Newsgroups: comp.lang.ada Subject: Question: Exporting ADA functions to C Date: Wed, 21 Mar 2001 17:16:57 +0100 Organization: T-Online Message-ID: <3AB8D3F9.FC6C6B9E@uundz.de> Reply-To: Tilman.Gloetzner@uundz.de Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: news.t-online.com 985191452 04 32708 Ok87EWXStHQcs 010321 16:17:32 X-Complaints-To: abuse@t-online.com X-Sender: 0931406720-0001@t-dialin.net X-Mailer: Mozilla 4.76 [de] (X11; U; Linux 2.4.0-64GB-SMP i686) X-Accept-Language: en Xref: supernews.google.com comp.lang.ada:5967 Date: 2001-03-21T17:16:57+01:00 List-Id: 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 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