comp.lang.ada
 help / color / mirror / Atom feed
From: Jan Andres <jandres@gmx.net>
Subject: Re: calling ada from c with an array param
Date: Sun, 20 Sep 2009 21:59:02 +0200
Date: 2009-09-20T21:59:02+02:00	[thread overview]
Message-ID: <h961i6$hl$01$1@news.t-online.com> (raw)
In-Reply-To: 0208bf86-cba9-45db-a43c-53c249bf9ae6@l34g2000vba.googlegroups.com

On 2009-09-20, Mike <michael_tabak@yahoo.com> wrote:
> I'm new to Ada so I'm trying to expand a simple c example that passed
> primitive types to an Ada
> function. I'm trying to call an ada procedure from a c program with an
> in out float array using GNAT. I read you should not use an
> unconstrained array so I tried the following
>
> The c program is
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> extern void adainit (void);
> extern void adafinal (void);
> extern int conjunc (float[], int);
>
> int main (int argc, char *argv[])
> {
>
>    int i;
>    float b[3] = {0.0, 0.0, 0.0};
>
>    adainit();
>
>    conjunc(a, 4);
>
>    for (i = 0; i < 3; i++)
>    {
>      printf("a[%d] = %f, b[%d] = %f\n", i, a[i], b[i]);
>    }
>
>    adafinal();
>
>  }
>
> The .ads file is
>
> --  conjunc.ads
> package conjunc is
>    type Float_Array is array (Integer range <>) of Float;
>    subtype Contrained_Float_Array is Float_Array (1 .. Integer'Last);
>    procedure conj (A: in out Constrained_Float_Array; Len: in
> Integer);
>    pragma Export (C, conj, "conj");
> end conjunc;
>
> and the procedure .adb file is
>
>  --  conjunc.adb
> package body conjunc is
>    type Float_Array is array (Integer range <>) of Float;
>    subtype Contrained_Float_Array is Float_Array (1 .. Integer'Last);
>    procedure conj (A: in out Constrained_Float_Array; Len :in Integer)
> is
>    begin
>        A(1) := 99;
>        A(2) := 100;
>        A(3) := 101;
>        A(4) := 102;
>    end conj;
> end conjunc;
>
>
> The procedure to build it is first gcc -c cjserver.c which works fine,
> then gnatmake -c conjunc.adb which gives me the following error:
>
> conjunc.adb:3:09: "Float_Array" conflicts with declaration at
> conjunc.ads:3
> conjunc.adb:4:12: "Contrained_Float_Array" conflicts with declaration
> at conjunc.ads:4

All declarations from the package spec (.ads file) are automatically
visible in the package body (.adb file), so there's no point in
repeating them there.

And, unlike C in some cases, Ada generally does not allow repeated
declarations of the same entity even if they are equivalent.

> conjunc.ads:5:30: "Constrained_Float_Array" is undefined (more
> references follow)
> conjunc.ads:5:30: possible misspelling of "Contrained_Float_Array"

It's true -- there is a typo (missing 's').

HTH
Jan
-- 
Jan Andres <jandres@gmx.net>



  reply	other threads:[~2009-09-20 19:59 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-20 19:38 calling ada from c with an array param Mike
2009-09-20 19:59 ` Jan Andres [this message]
2009-09-21  8:04 ` Dmitry A. Kazakov
2009-09-21  8:08   ` Dmitry A. Kazakov
2009-09-21 17:29 ` Jeffrey R. Carter
2009-09-21 19:58 ` sjw
replies disabled

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