comp.lang.ada
 help / color / mirror / Atom feed
* Import pragma question
@ 2011-03-23  8:25 Alex Mentis
  2011-03-23  8:45 ` Pascal Obry
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Alex Mentis @ 2011-03-23  8:25 UTC (permalink / raw)


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).

The C function get_char code is in the file cio.c, located in the same
directory as g-io.adb, but how does the pragma know this? Does it use
the convention parameter to check all .c files in the current directory
for a function of the specified external name? What if multiple .c
files in the current directory have a get_char function? Could the .c
file containing the code to be imported be located in a different
directory? If so, how does the import find it?

Thank you for helping me advance my understanding.

Alex



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Import pragma question
  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 16:51 ` Jeffrey Carter
  2 siblings, 0 replies; 6+ messages in thread
From: Pascal Obry @ 2011-03-23  8:45 UTC (permalink / raw)
  To: Alex Mentis


Alex,

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

By passing the corresponding object file or library to the linker.  No 
magic! With GNAT you can use:

    $ gnat make main.adb -largs -lmylib

And possibly the PATH to this library with -L<path>/<to>/<lib>.

Where mylib.a (or mylib .dll or .so) contains the symbols from the 
foreign language. Another option is to a add a pragma Linker_Options 
into one of the sources.

    pragma Linker_Options ("-lmylib");

Pascal.

-- 

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|    http://www.obry.net  -  http://v2p.fr.eu.org
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver keys.gnupg.net --recv-key F949BD3B




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Import pragma question
  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
  2 siblings, 1 reply; 6+ messages in thread
From: Pascal Obry @ 2011-03-23  8:45 UTC (permalink / raw)



Alex,

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

By passing the corresponding object file or library to the linker.  No 
magic! With GNAT you can use:

    $ gnat make main.adb -largs -lmylib

And possibly the PATH to this library with -L<path>/<to>/<lib>.

Where mylib.a (or mylib .dll or .so) contains the symbols from the 
foreign language. Another option is to a add a pragma Linker_Options 
into one of the sources.

    pragma Linker_Options ("-lmylib");

Pascal.

-- 

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|    http://www.obry.net  -  http://v2p.fr.eu.org
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver keys.gnupg.net --recv-key F949BD3B




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Import pragma question
  2011-03-23  8:45 ` Pascal Obry
@ 2011-03-23  9:06   ` Alex Mentis
  0 siblings, 0 replies; 6+ messages in thread
From: Alex Mentis @ 2011-03-23  9:06 UTC (permalink / raw)


Pascal Obry wrote:

 
> By passing the corresponding object file or library to the linker.
> No magic! With GNAT you can use:
> 
>    $ gnat make main.adb -largs -lmylib
> 
> And possibly the PATH to this library with -L<path>/<to>/<lib>.
> 

Thanks. I have to admit, I've never had to mess with linker options too
much in my little projects, so this is sort of new territory for me.

Alex




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Import pragma question
  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 16:51 ` Jeffrey Carter
  2011-03-23 17:40   ` Alex Mentis
  2 siblings, 1 reply; 6+ messages in thread
From: Jeffrey Carter @ 2011-03-23 16:51 UTC (permalink / raw)


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



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Import pragma question
  2011-03-23 16:51 ` Jeffrey Carter
@ 2011-03-23 17:40   ` Alex Mentis
  0 siblings, 0 replies; 6+ messages in thread
From: Alex Mentis @ 2011-03-23 17:40 UTC (permalink / raw)


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!



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2011-03-23 17:40 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox