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=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!news.unit0.net!peer01.am4!peer.am4.highwinds-media.com!peer01.fr7!futter-mich.highwinds-media.com!news.highwinds-media.com!fx14.am4.POSTED!not-for-mail Subject: Re: Please evaluate tiny binding that does not use Interfaces.C Newsgroups: comp.lang.ada References: <75d36301-5649-403b-b06f-7166196929c5@googlegroups.com> From: Per Sandberg User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-Version: 1.0 In-Reply-To: <75d36301-5649-403b-b06f-7166196929c5@googlegroups.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit Message-ID: X-Complaints-To: abuse@usenet.se NNTP-Posting-Date: Fri, 18 Aug 2017 05:18:48 UTC Organization: usenet.se Date: Fri, 18 Aug 2017 07:18:47 +0200 X-Received-Body-CRC: 234272512 X-Received-Bytes: 4963 Xref: news.eternal-september.org comp.lang.ada:47739 Date: 2017-08-18T07:18:47+02:00 List-Id: 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 > #include > > void * initscr_plus() { > setlocale(LC_CTYPE,""); > initscr() ; > return stdscr ; > } >