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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,60beaed5c57d54d X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news1.google.com!goblin2!goblin3!goblin.stu.neva.ru!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: "Alex Mentis" Newsgroups: comp.lang.ada Subject: Re: Import pragma question Date: Wed, 23 Mar 2011 17:40:59 +0000 (UTC) Organization: A noiseless patient Spider Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 7bit Injection-Date: Wed, 23 Mar 2011 17:40:59 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="TfaOIE1E70h9psK9x8LxRg"; logging-data="775"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX194yy5qky5ub4u+QsxOpTs/1mJVq+B0J8g=" User-Agent: XanaNews/1.19.1.269 Cancel-Lock: sha1:qKYA1pNp8lEeEuwlfqfTmiaTRj4= Xref: g2news2.google.com comp.lang.ada:19375 Date: 2011-03-23T17:40:59+00:00 List-Id: 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!