From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,633b377c6cd60b33 X-Google-Attributes: gid103376,public From: Simon Wright Subject: Re: Need Help Calling Ada From C Date: 1998/12/12 Message-ID: #1/1 X-Deja-AN: 421735730 X-NNTP-Posting-Host: pogner.demon.co.uk:158.152.70.98 References: X-Complaints-To: abuse@demon.net X-Trace: news.demon.co.uk 913535008 nnrp-08:9423 NO-IDENT pogner.demon.co.uk:158.152.70.98 Organization: At Home Newsgroups: comp.lang.ada Date: 1998-12-12T00:00:00+00:00 List-Id: 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!