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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,ad4585f2971e47c5 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!feeder.news-service.com!ecngs!feeder.ecngs.de!Xl.tags.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!local2.nntp.ams.giganews.com!nntp.bt.com!news.bt.com.POSTED!not-for-mail NNTP-Posting-Date: Sun, 20 Feb 2011 13:51:40 -0600 From: Brian Drummond Newsgroups: comp.lang.ada Subject: Re: Need some light on using Ada or not Date: Sun, 20 Feb 2011 19:54:30 +0000 Reply-To: brian@shapes.demon.co.uk Message-ID: References: <4d5ef836$0$23753$14726298@news.sunsite.dk> <7ibvl6tn4os3njo3p4kek9kop44nke3n7t@4ax.com> <4d605e67$0$23753$14726298@news.sunsite.dk> X-Newsreader: Forte Agent 1.7/32.534 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Usenet-Provider: http://www.giganews.com X-AuthenticatedUsername: NoAuthUser X-Trace: sv3-pS185X6b8Q5uN9bGOjwGPvjeG2wJ0snm/N3cQzgENd6ymOC9RWmUH34tntfvQQLN01+IastSLmICre4!rD6+Vv7kAvlXBMGhnx6YACBtzT8pa4BEyaKpN3ZzLrzdEb3LqiKpbF7ftD+h8wxPp8xUUq/TK5Gm!e3g= X-Complaints-To: abuse@btinternet.com X-DMCA-Complaints-To: abuse@btinternet.com X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 5193 Xref: g2news1.google.com comp.lang.ada:17492 Date: 2011-02-20T19:54:30+00:00 List-Id: On 20 Feb 2011 00:20:56 GMT, "Luis P. Mendes" wrote: >Sat, 19 Feb 2011 13:07:58 +0000, Brian Drummond escreveu: > >> Ada can easily bind to C libraries, it's standard and well documented. >> C++ bindings are also possible, but with some work and (currently) some >> limitations. >> A GCC recent enough to support "-f-dump-ada-spec" will auto-generate an >> Ada spec from C++ sources, which will save a lot of the work. > >Would you mind giving me an example? See below... >Please consider the following C++ code: >===== header file >$ cat aleatorio.h >===== source file >$ cat aleatorio.cpp >===== > >>From Ada, how can I use these h and cpp files to call, for example, >gerarAleatorioInteiro(0,10)? Here is what I did. 1) Comment out the #includes in aleatorio.h. They are unused; enlarge the namespace; and are repeated in the .cpp file anyway. Save it as aleatorio.hpp. (This forces C++-style Ada specs rather than C-style, which is essential to link to C++ code) 2) Generate the specs automatically. /usr/gnat/bin/gcc -fdump-ada-spec aleatorio.hpp produces an automatic spec file aleatorio_hpp.ads ------------------------------- with Interfaces.C; use Interfaces.C; package aleatorio_hpp is procedure iniciarSemente; -- aleatorio.hpp:8:21 pragma Import (CPP, iniciarSemente, "_Z14iniciarSementev"); function gerarAleatorio (a : int; b : int) return double; -- aleatorio.hpp:9:35 pragma Import (CPP, gerarAleatorio, "_Z14gerarAleatorioii"); function gerarAleatorioInteiro (a : int; b : int) return int; -- aleatorio.hpp:10:39 pragma Import (CPP, gerarAleatorioInteiro, "_Z21gerarAleatorioInteiroii"); function arredondar (res : double) return int; -- aleatorio.hpp:11:26 pragma Import (CPP, arredondar, "_Z10arredondard"); end aleatorio_hpp; ------------------------------- 3) Not essential but recommended ... Write a wrapper package to hide the C interface and C types, and to make the interface look like Ada: random_wrapper.ads, random_wrapper.adb. (This constitutes a "thick binding", while package aleatorio_h is a "thin binding") At this point you can choose what to expose to the Ada code; I have been selective (or lazy!) ------------ random_wrapper.ads -------------- package random_wrapper is procedure initialise_seed; function random_between(a,b : in Integer) return Integer; end random_wrapper; ------------ random_wrapper.adb -------------- with aleatorio_hpp; use aleatorio_hpp; with Interfaces.C; use Interfaces.C; package body random_wrapper is procedure initialise_seed is begin iniciarSemente; end initialise_seed; function random_between(a,b : in Integer) return Integer is begin return Integer(gerarAleatorioInteiro (int(a), int(b))); end random_between; end random_wrapper; ---------------------------------------------- 4) Write your Ada program... ------------ random.adb ---------------------- --Random number tester with Ada.Text_Io; use Ada.Text_Io; with Ada.Integer_Text_Io; use Ada.Integer_Text_Io; with random_wrapper; use random_wrapper; procedure random is begin initialise_seed; Put("Five random numbers"); New_Line; for i in 1 .. 5 loop Put(random_between(1,100)); New_Line; end loop; end random; ---------------------------------------------- 5) Compile the C++ portion (more complex examples may need a Makefile) g++ -g -m64 -c -o aleatorio.o aleatorio.cpp 6) Build the Ada portion. gnatmake -m64 -gnat05 -gnato -gnatwa -fstack-check -o random random.adb \ -largs ./aleatorio.o -lstdc++ Note additional arguments "-largs ./aleatorio.o -lstdc++" to gnatlink; extend these if you add more C++ objects and libraries. 7) Run it. ./random Five random numbers 9 40 2 77 66