comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Wright <simon@pogner.demon.co.uk>
Subject: Re: Need Help Calling Ada From C
Date: 1998/12/12
Date: 1998-12-12T00:00:00+00:00	[thread overview]
Message-ID: <x7v4sr1u4c1.fsf@pogner.demon.co.uk> (raw)
In-Reply-To: hoffman-ya02408000R0512981659060001@news.insync.net

hoffman@insync.net (David Hoffman) writes:

> 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'

If you look at the C file generated by gnatbind (b_first_test.c) you
will find at the end a section like the example below, which tells
what the linker needs to see:

/* BEGIN Object file/option list
./buggy.o
-L./
-L/u1/simon/gnat/lib/gcc-lib/i386-linux/2.7.2.1/adalib/
-L/usr/lib/gcc-lib/i386-linux/2.7.2.1/adalib/
-lgnat
   END Object file/option list */

You need to give the bit between the BEGIN and END lines to the
linker. I've used GNU Make rules as below to create a 'response' file:

b_%.c: %.ali
	gnatbind -n $(DEB) $(ADAINCLUDES) $<

b_%.o: b_%.c
	gcc -c $<

%.rsp: %.c
	sed -n -e "/BEGIN/,/END/p" < $*.c | egrep -v "BEGIN|END" > $*.rsp

and then (foo.o comes from foo.c, foo_body is written in Ada):

foo: b_foo_body.rsp b_foo_body.o foo.o
	gcc $(LDFLAGS) -o foo foo.o b_foo_body.o \
	  `cat b_foo_body.rsp` \
          (... other stuff)

I'm perfectly sure there's a better way of generating that response file!





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

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

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