comp.lang.ada
 help / color / mirror / Atom feed
From: hoffman@insync.net (David Hoffman)
Subject: Need Help Calling Ada From C
Date: 1998/12/05
Date: 1998-12-05T00:00:00+00:00	[thread overview]
Message-ID: <hoffman-ya02408000R0512981659060001@news.insync.net> (raw)

Hi,

I am trying to figure out how to call an Ada function from a C program.
For the record I am using GNAT 3.10p on Linux (Slackware, kernel 2.0.33). 
I read what I could find on the net, in the GNAT manual, and in several 
Ada books that I have. Since I did not find a complete, step-by-step ex-
ample I had to make a few guesses (and maybe that is where things went 
wrong).

I created a C program, called fact.c, that calls an Ada function from a
package called first_test (the source code for these items is given at
the end of this message). I compiled this with the commands:

$ gcc -c first_test.ads
$ gcc -c first_test.adb
$ gnatbind -n first_test.ali
$ gcc -c b_first_test.c
$ gcc -c fact.c

I made it this far without any real trouble. Then I got this:

$ gcc -o fact *.o
b_first_test.o: In function `adainit':
b_first_test.o(.text+0x26): undefined reference to `__gnat_set_globals'
b_first_test.o(.text+0x2e): undefined reference to `system__elabs'
b_first_test.o(.text+0x33): undefined reference to
`system__exception_table___elabb'
b_first_test.o(.text+0x38): undefined reference to `interfaces__c___elabs'
b_first_test.o(.text+0x3d): undefined reference to
`system__tasking_soft_links___elabb'
b_first_test.o(.text+0x42): undefined reference to
`system__secondary_stack___elabb'
fact.o: In function `main'
fact.o(.text+0x30): undefined reference to `fact'

Where did I go wrong?

David Hoffman
david.a.hoffman1@jsc.nasa.gov

fact.c --------------------------------------------------------------------

extern void adainit();
extern void adafinal();
extern long factorial( long N );

main (argc, argv)
  char *argv[];
   int argc;
{
   long answer, value;
   
   adainit();
 
   value = atol(argv[1]);

   if ( value 0 ) 
   {
      if ( value <= 12 ) 
      {
         answer = factorial(value);
         printf("\n  %2d! = %3d\n",value,answer);
      }
      else 
      {
         printf("\n  fact: input value too big (would cause
overflow).\n");
      }
   }
   else 
   {
      printf("\n  fact: input must be strictly positive.\n");
   }
   
   adafinal();

}

first_test.ads ---------------------------------------------------------

with Interfaces.C;  use Interfaces;

package First_Test is

  function Factorial ( Value : C.long ) return C.long;
  
  pragma Export (C, Factorial, "factorial");

end First_Test;

first_test.adb ---------------------------------------------------------

with Interfaces.C;   use Interfaces;

first_test.adb

package body First_Test is

  function Ada_Factorial (N : Integer) return Integer is
  begin 
     if N = 1 then
        return 1;
     else
        return N * Ada_Factorial(N-1);
     end if;
  end;

  function Factorial (Value : C.long) return C.long is
     P,Q : Integer;
  begin
     P := Integer(Value);
     Q := Ada_Factorial(P);
     return C.long(Q);
  end;

end First_Test;




             reply	other threads:[~1998-12-05  0:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-12-05  0:00 David Hoffman [this message]
1998-12-09  0:00 ` Need Help Calling Ada From C Mats Weber
1998-12-12  0:00 ` Simon Wright
replies disabled

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