comp.lang.ada
 help / color / mirror / Atom feed
From: "Alex Mentis" <foo@invalid.invalid>
Subject: Re: Import pragma question
Date: Wed, 23 Mar 2011 17:40:59 +0000 (UTC)
Date: 2011-03-23T17:40:59+00:00	[thread overview]
Message-ID: <imdbba$o7$1@news.eternal-september.org> (raw)
In-Reply-To: imd8vu$is$1@tornado.tornevall.net

Jeffrey Carter wrote:

> On 03/23/2011 01:25 AM, Alex Mentis wrote:
> > I've never interfaced Ada with other languages, so I have some
> > questions about pragma Import. How does an import pragma know where
> > to find the non-Ada code to be used for the import?
> > 
> > As an illustration of my question, I'll use the GNAT.IO package
> > (g-io.adb) from GNAT 4.3.6.
> > 
> > In g-io.adb, pragma Import is used to import a C function "get_char"
> > and associate it with the Ada function Get_Char (lines 53-54).
> 
> You have to link your Ada program with the object code produced by
> the C compiler. That object code contains a symbolic name for the
> function to allow callers to call the right location. The C name
> given in the pragma is matched to the corresponding symbolic name in
> the C object code.
> 
> How you link with the C object code depends on your compiler and
> linker. Pragma Linker_Options provides a semi-portable way to specify
> this.
> 
> Suppose you have a C function "mine" in mine.c:
> 
> int mine (int i){...}
> 
> and some Ada that imports it:
> 
> pragma Linker_Options ("mine.o");
> 
> function Yours (I : Integer) return Integer is
>    subtype Int is Interfaces.C.Int;
> 
>    function Mine (I : Int) return Int;
>    pragma Import (C, Mine, "mine");
> begin -- Yours
>    return Integer (Mine (Int (I) ) );
> end Yours;
> 
> This should work for GNAT on Linux. You compile mine.c to produce
> mine.o. Then you can make the code containing Yours, and it will link
> mine.o into the executable and match it to the imported function Mine.

Thanks for the example!



      reply	other threads:[~2011-03-23 17:40 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-23  8:25 Import pragma question Alex Mentis
2011-03-23  8:45 ` Pascal Obry
2011-03-23  8:45 ` Pascal Obry
2011-03-23  9:06   ` Alex Mentis
2011-03-23 16:51 ` Jeffrey Carter
2011-03-23 17:40   ` Alex Mentis [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