comp.lang.ada
 help / color / mirror / Atom feed
* Help with access-to-functions and imported C functions
@ 1997-08-15  0:00 Matthew Kennedy
  1997-08-20  0:00 ` Stephen Leake
  0 siblings, 1 reply; 2+ messages in thread
From: Matthew Kennedy @ 1997-08-15  0:00 UTC (permalink / 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




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Help with access-to-functions and imported C functions
  1997-08-15  0:00 Help with access-to-functions and imported C functions Matthew Kennedy
@ 1997-08-20  0:00 ` Stephen Leake
  0 siblings, 0 replies; 2+ messages in thread
From: Stephen Leake @ 1997-08-20  0:00 UTC (permalink / raw)



Matthew Kennedy wrote:
> 
> ...
> 
> 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.
>
> ...
>    Random := Random_Gaussian'Access;   -- gives compile error
> "subprogram has invalid convention for context"
> 

As this message is (somewhat cryptically) trying to tell you, you have
specified different calling conventions for Random and Random_Gaussian.
You need to use "pragma Convention". Note that you must apply the pragma
to both the access type, and the function "Two":

with Ada.Text_IO, Interfaces.C;
use  Ada.Text_IO, Interfaces.C;
procedure Test
is
   package Double_IO is new Ada.Text_IO.Float_IO(Double);
   use Double_IO;

   type Function_Access is access function return Double;
   pragma Convention (C, Function_Access);

   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;
   pragma Convention (C, Two);

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

   Random : Function_Access;
   Y : Double;

begin
   Random := Two'Access;
   Random := Random_Gaussian'Access;
   Y := Random.all;
   Put(Y);
   New_Line;
end Test;

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

-- 
- Stephe




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1997-08-20  0:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-08-15  0:00 Help with access-to-functions and imported C functions Matthew Kennedy
1997-08-20  0:00 ` Stephen Leake

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