comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew Kennedy <q957722@mail.connect.usq.edu.au>
Subject: Help with access-to-functions and imported C functions
Date: 1997/08/15
Date: 1997-08-15T00:00:00+00:00	[thread overview]
Message-ID: <33F31D5B.71B2@mail.connect.usq.edu.au> (raw)


Hello,

I have this program which must sample either a Gaussian or a uniform
random distribution. The functions to return the random numbers for each
distribution are already defined in a file called random.c which has
been compiled into random.o ready to link into my Ada program.

I need the program to use either the Uniform or Gaussian functions
depending on some user input at run-time so I tried to create a variable
which is an access to a type of the same type as the C functions.

It seems how ever, that I cannot assign the access of an imported C
function (please see code below for the exact error message) to an
access variable of the same 'type' of function. Doesn't Ada support such
constructs? (I couldn't find the answer in the ARM)

(Note the program below is just a representitive illustration of the
actual problem)

Thank you,
Matt.


with Ada.Text_IO, Interfaces.C;
use  Ada.Text_IO, Interfaces.C;

-- COMPILE COMMAND: 
--   gcc -c random.c -o random.o
--   gnatmake distribution -largs random.o

procedure Distribution is

   package Double_IO is new Ada.Text_IO.Float_IO(Double); 
   use Double_IO;

   type Function_Access is access function return Double;

   function Random_Gaussian return Double; -- double
RandomGaussian(void);
   function Random_Uniform return Double;  -- double
RandomUniform(void);

   pragma Import(C, Random_Gaussian, "RandomGaussian");
   pragma Import(C, Random_Uniform, "RandomUniform");

   function Two return Double is
   begin
      return 2.0;
   end Two;

   Random : Function_Access;
   Y : Double;

begin
   Random := Two'Access;               -- this line works, but the
following line
   Random := Random_Gaussian'Access;   -- gives compile error
"subprogram has invalid convention for context"
   Y := Random.all;
   Put(Y); 
   New_Line;
end Distribution;


-- 
Matthew Kennedy
Student of Electronics Engineering
University of Southern Queensland, Australia
  "When will you realise your already there?" 
      - Marilyn Manson




             reply	other threads:[~1997-08-15  0:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-08-15  0:00 Matthew Kennedy [this message]
1997-08-20  0:00 ` Help with access-to-functions and imported C functions 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