comp.lang.ada
 help / color / mirror / Atom feed
From: Gautier <gautier@fakeaddress.nil>
Subject: Re: C Interface example
Date: Sat, 03 Feb 2007 18:48:52 +0100
Date: 2007-02-03T18:48:52+01:00	[thread overview]
Message-ID: <45c4cae7$1_4@news.bluewin.ch> (raw)
In-Reply-To: <1170516037.435847.326440@p10g2000cwp.googlegroups.com>

artifact.one@googlemail.com:

> I'm having a bit of trouble getting my head around calling C code
> in Ada. I'll spare you my failed attempts, basically I have this code:
> 
> (vector.h)
> 
> #ifndef VECTOR_H
> #define VECTOR_H
> 
> float *vec_addNf(float *, float *, unsigned int);
> 
> #endif
> 
> (vec_add.c)
> 
> #include "vector.h"
> 
> float *vec_add(float *va, float *vb, unsigned int ne)
> {
>   unsigned int ind;
>   for (ind = 0; ind < ne; ++ind)
>     va[ind] += vb[ind];
>   return va;
> }
> 
> Now what would be the most simple and portable way to write
> a vector,ads and vector.adb file? (Specification and body, in case
> those are compiler-specific filenames, I'm not intimate with the
> Ada language spec yet!). I just don't have a good enough
> understanding of the language to see how it all fits together
> yet and I usually learn by example.

Looking at your code the most simple and portable way will be not to interface 
at all with C!

package Vectors is

   type Vector is array(Natural range <>) of Float;

   procedure Add(va: in out Vector; vb: in Vector);

end Vectors;

package body Vectors is

   procedure Add(va: in out Vector; vb: in Vector) is
   begin
     for i in va'Range loop
       va(i):= va(i) + vb(i);
     end loop;
   end Add;

end Vectors;

Note that most compilers accept the spec and the body (or more!) in the same 
file with the name of your choice; GNAT requires vectors.ads and vectors.adb
HTH
______________________________________________________________
Gautier         -- http://www.mysunrise.ch/users/gdm/index.htm
Ada programming -- http://www.mysunrise.ch/users/gdm/gsoft.htm

NB: For a direct answer, e-mail address on the Web site!



  parent reply	other threads:[~2007-02-03 17:48 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-02-03 15:20 C Interface example artifact.one
2007-02-03 16:15 ` Ludovic Brenta
2007-02-03 17:15   ` Simon Wright
2007-02-03 19:43     ` Ludovic Brenta
2007-02-03 17:39   ` artifact.one
2007-02-04 14:34     ` Stephen Leake
2007-02-03 16:19 ` Cesar Rabak
2007-02-03 17:40   ` artifact.one
2007-02-03 19:59     ` Cesar Rabak
2007-02-03 17:48 ` Gautier [this message]
2007-02-03 18:09   ` artifact.one
2007-02-03 19:17     ` Georg Bauhaus
2007-02-03 19:53       ` artifact.one
2007-02-03 20:02         ` Gautier
2007-02-03 21:36           ` Georg Bauhaus
2007-02-03 19:48     ` tmoran
2007-02-03 19:55       ` artifact.one
2007-02-04 14:37     ` Stephen Leake
2007-02-03 20:42   ` Pascal Obry
2007-02-03 20:52 ` Jeffrey R. Carter
2007-02-03 20:57   ` artifact.one
2007-02-04  4:05     ` Jeffrey R. Carter
2007-02-04 11:52   ` Simon Wright
2007-02-04 20:59     ` Jeffrey R. Carter
2007-02-05  8:56   ` Maciej Sobczak
2007-02-05 18:12     ` Jeffrey R. Carter
2007-02-06  1:48       ` Randy Brukardt
2007-02-06  8:20         ` Maciej Sobczak
2007-02-06 19:18         ` Jeffrey R. Carter
2007-02-04 14:32 ` Stephen Leake
replies disabled

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