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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,c1bdceb867926fdb X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!usenet-fr.net!gegeweb.org!aioe.org!not-for-mail From: tmoran@acm.org Newsgroups: comp.lang.ada Subject: Re: Interfacing Ada with C Date: Sun, 25 Jul 2010 00:22:11 +0000 (UTC) Organization: Aioe.org NNTP Server Message-ID: References: NNTP-Posting-Host: J4HSNf9Eqj44wTz1J3b8lQ.user.speranza.aioe.org X-Complaints-To: abuse@aioe.org X-Notice: Filtered by postfilter v. 0.8.2 X-Newsreader: Tom's custom newsreader Xref: g2news1.google.com comp.lang.ada:12535 Date: 2010-07-25T00:22:11+00:00 List-Id: > On Jul 24, 6:44=A0pm, "Dmitry A. Kazakov" > wrote: > > > Since C does not have arrays, especially 2D ones, you will have some cust= > om > > representation on the C side. It means that examples of bindings (there a= > re > > plenty of) won't help you unless you understand how you describe a > > C-compatible object in Ada. > > > Thanks for the "warnings". One way that I'm thinking is to write the > elements of the matrix sequentially into a file in Ada, read them into > C, put them into a matrix in C, do the appropriate computations in C > (with the IMSL C library), write the output in C in a file, and > finally importing them back into Ada again for further processing. Why do somersaults when you can just walk a few steps. As Dmitry says, it really does depend on just what you are supposed to pass to the C routine. For instance, there's a very fast C routine to calculate an FFT of a 256 element complex vector. Using type Complex_Vectors is array(Integer range <>) of Ada.Numerics.Complex_Types.Complex; subtype Complex_256s is Complex_Vectors(1 .. 256); procedure fftc4_256(V : in out Complex_256s); pragma import(c, fftc4_256,"fftc4_256"); it can be called with X : Complex_256s; ... fftc4(X); Hard to get any simpler. If the C routine wants a pointer to an array of pointers to rows, plus a couple of dimensions, that's a little harder. But if the array is stored simply, and C expects the elements laid out the same way as Ada, you can probably just pass in a pointer to the first element in the array: type Matrix_Type is array(Integer Range <>, Integer range <>) of aliased Ada.Numerics.Complex_Types.Complex; type Pointer_To_Element is access all Ada.Numerics.Complex_Types.Complex; procedure C_Routine(M : in Pointer_To_Element; Height, Width : in Interfaces.C.Unsigned); pragma import(C, C_Routine, "c_routine"); M : Matrix_Type(1 .. 10, 0 .. 100); ... C_Routine(M(M'first(1), M'first(2))'access, Height => M'length(1), Width => M'length(2)); For a whole lot of examples of Ada calling C, download the source for CLAW (Class Library for Ada Windows) from www.rrsoftware.com