comp.lang.ada
 help / color / mirror / Atom feed
From: Per Sandberg <per.s.sandberg@bahnhof.se>
Subject: Re: Please evaluate tiny binding that does not use Interfaces.C
Date: Fri, 18 Aug 2017 07:18:47 +0200
Date: 2017-08-18T07:18:47+02:00	[thread overview]
Message-ID: <YMulB.575806$FB3.328661@fx14.am4> (raw)
In-Reply-To: <75d36301-5649-403b-b06f-7166196929c5@googlegroups.com>

Hi

The proper way to generate bindings to libraries with interfaces defined 
in C/C++ is to use the compiler-switch -fdump-ada-spec. To trust a human 
to get all the bits and pieces correct only works if the interface is 
trivial.
The way I usually does it is:

* generate valid a C/C++ file including all required headers.
* compile the file with gcc -c -fdfump-ada-spec ${file}
* If required, edit in the file using some script tool such as sed.

By using the above method you could regenerate the bindings when the 
underlying library evolves and you will get a correct binding every time.

Of corse if you want to use one and only one simple method from a 
foreign library its always possible to just do a simple "import" in the 
code.

/P

Den 2017-08-18 kl. 04:23, skrev patrick@spellingbeewinnars.org:
> Hi Everyone
> 
> It seems like everyone uses Interfaces.C when creating a C binding. I realize that this makes the binding more portable.
> 
> However, is there something to be said for small, easy to read code too?
> 
> It seems that most bindings attempt to recreate C structs with records and then create pointers to those records. Meanwhile, the binding does not access any members of that C struct directly. Those structs often have other structs as members too and the process goes on and on.
> 
> I realize that void pointers are dangerous but C is dangerous and I am wondering if my use is within good practices?
> 
> My code is small and contains no exception handling and likely has lots of other things missing like a gpr files and such but is the basic interfacing between C and Ada okay? Do you see any pitfalls, aside from the fact that it is calling C ?
> 
> I personally find this very easy to write and read, I am hoping that this will work out. Please let me know what you think
> 
> -----------------------------------------------------------------
> build.sh
> 
> gnatmake -c terminal_app.adb
> gnatmake -c ncurses_glue
> 
> gcc-4.6 -c ncurses_glue.c
> 
> gnatbind terminal_app.ali ncurses_glue.ali
> 
> gnatmake -c b~terminal_app.adb
> gcc-4.6 terminal_app.o  b~terminal_app.o ncurses_glue.o -o terminal_app  -gnatwa -lncursesw -lgnat -gnato
> 
> -----------------------------------------------------------------
>        -- test to see if library without interfaces.c will work out
>        --
>        --
>           with ncurses_glue ;
>           with ada.strings.fixed ;
>           
>           procedure terminal_app is
>   
>           package ng renames ncurses_glue ;
>           package sf renames ada.strings.fixed ;
>   
>           stdscr : access ng.WINDOW ;
>           ret    : integer ;
>           str    : string := "test 㐴    " & ASCII.NUL ;
>   
>           begin
> 
>            stdscr := ng.initscr_plus ;
>            ret    := ng.addstr(str) ;
>            ret    := ng.refresh ;
>            delay 2.0 ;
>            ret    := ng.endwin ;
> 
>           end terminal_app ;
> 
> -----------------------------------------------------------------
> 
>           package ncurses_glue is
> 
>           type null_record is null record ;
> 
>           type WINDOW is access null_record ;
> 
>           function initscr_plus
>                    return Access WINDOW;
>                    pragma Import (C, initscr_plus, "initscr_plus");
> 
>           function addstr (arg1 : string)
>                    return integer ;
>                    pragma Import (C, addstr, "addstr");
> 
>           function refresh
>                    return integer;
>                    pragma Import (C, refresh, "refresh") ;
> 
>           function endwin
>                    return integer;
>                    pragma Import (C, endwin, "endwin") ;
> 
>           end ncurses_glue ;
> -----------------------------------------------------------------
> #include <ncursesw/ncurses.h>
> #include <locale.h>
> 
> void * initscr_plus() {
>    setlocale(LC_CTYPE,"");
>    initscr() ;
>    return stdscr ;
>   }
> 

  reply	other threads:[~2017-08-18  5:18 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-18  2:23 Please evaluate tiny binding that does not use Interfaces.C patrick
2017-08-18  5:18 ` Per Sandberg [this message]
2017-08-18 13:05   ` patrick
2017-08-18 15:41     ` patrick
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox