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!news3.google.com!feeder.news-service.com!94.75.214.39.MISMATCH!aioe.org!news.tornevall.net!.POSTED!not-for-mail From: Jeffrey Carter Newsgroups: comp.lang.ada Subject: Re: Import pragma question Date: Wed, 23 Mar 2011 09:51:35 -0700 Organization: TornevallNET - http://news.tornevall.net Message-ID: References: NNTP-Posting-Host: 4e5e299d3e045c6ebae7688001413804 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: 5e54fdbe222ffb6dc046afa0a08d74ee X-Complaints-To: abuse@tornevall.net User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.14) Gecko/20110223 Thunderbird/3.1.8 X-Complaints-Language: Spoken language is english or swedish - NOT ITALIAN, FRENCH, GERMAN OR ANY OTHER LANGUAGE! In-Reply-To: X-UserIDNumber: 1738 X-Validate-Post: http://news.tornevall.net/validate.php?trace=5e54fdbe222ffb6dc046afa0a08d74ee X-Complaints-Italiano: Non abbiamo padronanza della lingua italiana - se mandate una email scrivete solo in Inglese, grazie X-Posting-User: 0243687135df8c4b260dd4a9a93c79bd Xref: g2news2.google.com comp.lang.ada:19374 Date: 2011-03-23T09:51:35-07:00 List-Id: 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