comp.lang.ada
 help / color / mirror / Atom feed
From: igor.kh@gmail.com
Subject: Returning data from Ada to C
Date: 3 May 2005 16:49:52 -0700
Date: 2005-05-03T16:49:52-07:00	[thread overview]
Message-ID: <1115164192.585409.163780@z14g2000cwz.googlegroups.com> (raw)

I want to make use of an Ada package from some C code. I already have
something that does almost what I want, but I need to figure out how to
return some data from Ada to C. I've never worked with Ada before, so
please pardon my ignorance.

Here's the setup. I have a C file:

---- main.c ----
#include <stdio.h>

extern void adainit();
extern void adafinal();
extern void _ada_phc_direct_solver ( ..., int *rcnt, double **solvec );

int main ()
{
  int rcnt;
  double *solvec;
  int i, j, n;

  /* Do some initializations. */
  ...

  /* I want the Ada code to store a pointer in `solvec'. */
  adainit();
  _ada_phc_direct_solver(..., &rcnt, &solvec);
  adafinal();
  /* BTW, is it OK to use Ada allocated memory after `adafinal()'? */

  /* Print results. */
  printf ("Root count: %d\n", rcnt);
  printf ("Roots: Re Im\n");
  for (i=0; i<rcnt; i++)
  {
    for (j=0; j<n; j++)
      printf ("%f %f\n", solvec[rcnt*n+2*j], solvec[rcnt*n+2*j+1]);
    printf ("\n");
  }

  return 0;
}
----------------

The Ada package uses its own suite of types to interface to C. I'll try
to provide their definitions as I understand them. The Ada side of this
looks like this:

---- phc_direct_solver.adb ----
procedure phc_direct_solver (..., rcnt : out integer;
		             solvec : out C_dblarrs.Pointer ) is

-- I'm not really sure how C_dblarrs.Pointer is defined, the closest
-- definition I can find is
--
--  type C_Double_Array is
--    array ( Interfaces.C.size_t range <> ) of aliased
Interfaces.C.double;
--
--  package C_DblArrs is
--    new Interfaces.C.Pointers(Interfaces.C.size_t,
--                              Interfaces.C.double,
--                              C_Double_Array,0.0);
--
-- Perhaps I should be using another type, but I don't know which one.

  rc : integer;
  sols : C_Double_Array; -- This is probably wrong, but `sols' will be
                         -- a C_Double_Array of some size.

begin
-- Calculate `rc' and `sols'.
  ...

-- Now I want to return `rcnt' and `sols'
  rcnt := rc;                -- This is the easy part.
  solvec := ???              -- What goes here?

end phc_direct_solver;
-------------------------------

Basically, the Ada code creates an array which I want pass back to the
C
code through a pointer. Unfortunately, I'm not familiar enough with Ada
figure out how to do that. Any help or suggestions would be
appreciated.

A related question. I'm using GNAT as the compiler. I read the section
in the documentation about linking a main C program with Ada auxiliary
Ada code. But it looks like the linking can only be done with gnatlink
at the final stage, when the executable is created. Is there a way to
compile the Ada code directly into a .o or .so object file that can be
linked normally with gcc?

Thanks in advance.

Igor

P.S.: For those who are curious, I'm trying to make use of PHCpack[1]
to
solve a system of polynomial equations. I'm working from the existing
example ts_phc_solver.c.

[1] http://www2.math.uic.edu/~jan/download.html




             reply	other threads:[~2005-05-03 23:49 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-05-03 23:49 igor.kh [this message]
2005-05-04  4:49 ` Returning data from Ada to C Jeffrey Carter
2005-05-04 13:45   ` Igor Khavkine
2005-05-04 13:58     ` Alex R. Mosteo
2005-05-06  3:36     ` Jeffrey Carter
2005-05-07 22:12       ` igor.kh
2005-05-08  2:07         ` Jeffrey Carter
2005-05-08 19:58           ` Jeffrey Carter
2005-05-08  9:41         ` Martin Dowie
2005-05-08 14:55           ` Ludovic Brenta
2005-05-08 14:58             ` Ludovic Brenta
2005-05-09 22:49               ` igor.kh
2005-05-10 12:48                 ` Ludovic Brenta
2005-05-16  4:35   ` Dave Thompson
2005-05-16  5:03     ` Jeffrey Carter
replies disabled

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