From: "Luis P. Mendes" <luislupeXXX@gmailXXX.com>
Subject: Re: Need some light on using Ada or not
Date: 23 Feb 2011 22:19:50 GMT
Date: 2011-02-23T22:19:50+00:00 [thread overview]
Message-ID: <4d658805$0$23752$14726298@news.sunsite.dk> (raw)
In-Reply-To: aur2m6p0lhfltiqg21fh8vf3l9ntl29n2i@4ax.com
Sun, 20 Feb 2011 19:54:30 +0000, Brian Drummond escreveu:
> On 20 Feb 2011 00:20:56 GMT, "Luis P. Mendes" <luislupeXXX@gmailXXX.com>
> 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
next prev parent reply other threads:[~2011-02-23 22:19 UTC|newest]
Thread overview: 46+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-02-18 22:52 Need some light on using Ada or not Luis P. Mendes
2011-02-18 23:58 ` Georg Bauhaus
2011-02-19 14:25 ` Simon Wright
2011-02-19 0:20 ` Edward Fish
2011-02-20 0:13 ` Luis P. Mendes
2011-02-20 1:36 ` Marc A. Criley
2011-02-20 9:59 ` mockturtle
2011-02-20 10:37 ` Brian Drummond
2011-02-20 11:08 ` Ludovic Brenta
2011-03-01 8:10 ` Adrian Hoe
2011-03-01 8:29 ` Thomas Løcke
2011-03-04 13:34 ` Adrian Hoe
2011-02-19 8:43 ` Vadim Godunko
2011-02-19 13:07 ` Brian Drummond
2011-02-19 14:17 ` Simon Wright
2011-02-19 18:02 ` Brian Drummond
2011-02-19 18:07 ` Bill Findlay
2011-02-20 10:42 ` Brian Drummond
2011-02-19 14:36 ` Georg Bauhaus
2011-02-19 18:25 ` Brian Drummond
2011-02-20 14:34 ` Brian Drummond
2011-02-20 15:45 ` jonathan
2011-02-20 16:18 ` Brian Drummond
2011-02-20 19:49 ` Pascal Obry
2011-02-20 19:57 ` Brian Drummond
2011-02-20 20:10 ` jonathan
2011-02-20 21:15 ` Pascal Obry
2011-02-20 21:26 ` Vinzent Hoefler
2011-02-20 21:33 ` Vinzent Hoefler
2011-02-20 21:36 ` Pascal Obry
2011-02-20 21:50 ` Vinzent Hoefler
2011-02-20 22:18 ` jonathan
2011-02-20 22:47 ` Simon Wright
2011-02-21 12:52 ` Brian Drummond
2011-02-21 13:44 ` Simon Wright
2011-02-24 0:19 ` Brian Drummond
2011-02-24 7:41 ` Jacob Sparre Andersen
2011-02-22 2:15 ` Shark8
2011-02-20 16:42 ` jonathan
2011-02-20 20:02 ` Brian Drummond
2011-02-20 0:20 ` Luis P. Mendes
2011-02-20 10:50 ` Brian Drummond
2011-02-20 19:54 ` Brian Drummond
2011-02-23 22:19 ` Luis P. Mendes [this message]
2011-02-24 17:06 ` Brian Drummond
2011-02-27 17:51 ` Luis P. Mendes
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox