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,ad4585f2971e47c5 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news4.google.com!feeder2.cambriumusenet.nl!feed.tweaknews.nl!194.134.4.91.MISMATCH!news2.euro.net!multikabel.net!newsfeed20.multikabel.net!feeder.erje.net!nuzba.szn.dk!news.szn.dk!pnx.dk!dotsrc.org!filter.dotsrc.org!news.dotsrc.org!not-for-mail From: "Luis P. Mendes" Subject: Re: Need some light on using Ada or not Newsgroups: comp.lang.ada References: <4d5ef836$0$23753$14726298@news.sunsite.dk> <7ibvl6tn4os3njo3p4kek9kop44nke3n7t@4ax.com> <4d605e67$0$23753$14726298@news.sunsite.dk> User-Agent: Pan/0.133 (House of Butterflies) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Date: 23 Feb 2011 22:19:50 GMT Message-ID: <4d658805$0$23752$14726298@news.sunsite.dk> Organization: SunSITE.dk - Supporting Open source NNTP-Posting-Host: 89.180.54.19 X-Trace: news.sunsite.dk DXC=ic7aSn4A^^V5LoVIWDNeH\YSB=nbEKnk[5DMU8coHF@\EHH=C_KSIRWlc:4cA5P5iUY61G6h^GXkQ4SiKofLn<[X2O?^?`m] Sun, 20 Feb 2011 19:54:30 +0000, Brian Drummond escreveu: > 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 Thank you very much Brian! I guess that this example or another one like this could be included in wiki or other place for newcomers. Luis