comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: Import pragma question
Date: Wed, 23 Mar 2011 09:51:35 -0700
Date: 2011-03-23T09:51:35-07:00	[thread overview]
Message-ID: <imd8vu$is$1@tornado.tornevall.net> (raw)
In-Reply-To: <imcaor$u2l$1@news.eternal-september.org>

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.

-- 
Jeff Carter
"This trial is a travesty. It's a travesty of a mockery of a
sham of a mockery of a travesty of two mockeries of a sham. ...
Do you realize there's not a single homosexual on that jury?"
Bananas
27



  parent reply	other threads:[~2011-03-23 16:51 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 [this message]
2011-03-23 17:40   ` Alex Mentis
replies disabled

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