FFI simple bindings attempt

"cedlemo [email protected] [ocaml_beginners]" <[email protected]> Wed, 13 Jul 2016 15:45:00 +0200
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <[email protected]>
Hi,


I wanted to try out the OCaml FFI and bind some C functions like the 
ncurses tutorial but I choose the ecore_evas (efl) instead:

here are the functions that I wanted to use:


void ecore_main_loop_quit(void)

void ecore_main_loop_begin(void)

int ecore_evas_init(void)

int ecore_evas_shutdown(void)

Ecore_Evas * ecore_evas_new(const char *, int, int, int, int, const char *)

ecore_evas_title_set(const Ecore_Evas *, const char *)

ecore_evas_show(ee)

ecore_evas_free(ee)


So I create the file ecore_evas.ml with this:

open Ctypes

open Foreign

let ecore_main_loop_begin = foreign "ecore_main_loop_begin" (void @-> 
returning void)

let ecore_main_loop_quit = foreign "ecore_main_loop_quit" (void @-> 
returning void)

let ecore_evas_init = foreign "ecore_evas_init" (void @-> returning int)

let ecore_evas_shutdown = foreign "ecore_evas_shutdown" (void @-> 
returning int)

type ecore_evas = unit ptr

let ecore_evas : ecore_evas typ = ptr void

let ecore_evas_new = foreign "ecore_evas_new" (string @-> int @-> int 
@-> int @-> int @-> string @-> returning ecore_evas)

let ecore_evas_title_set = foreign "ecore_evas_title_set" (ecore_evas 
@-> string @-> returning void)

let ecore_evas_show = foreign "ecore_evas_show" (ecore_evas @-> 
returning void)

let ecore_evas_free = foreign "ecore_evas_free" (ecore_evas @-> 
returning void)


then I created a mli file with those commands :

corebuild -pkg ctypes.foreign ecore_evas.inferred.mli
cp _build/ecore_evas.inferred.mli ./

the ecore_evas.inferred.mli


val ecore_main_loop_begin : unit -> unit

val ecore_main_loop_quit : unit -> unit

val ecore_evas_init : unit -> int

val ecore_evas_shutdown : unit -> int

type ecore_evas = unit Ctypes.ptr

val ecore_evas : ecore_evas Ctypes_static.typ

val ecore_evas_new : bytes -> int -> int -> int -> int -> bytes -> 
ecore_evas

val ecore_evas_title_set : ecore_evas -> bytes -> unit

val ecore_evas_show : ecore_evas -> unit

val ecore_evas_free : ecore_evas -> unit


And I created a ecore_evas_window.ml with :


open Ecore_evas

let () =

   ecore_main_loop_begin ();

   ecore_evas_init ();

   let ee = ecore_evas_new "" 50 50 300 300 "" in

     ecore_evas_title_set ee "This is a test";

     ecore_evas_show ee


Then I try to compile with

corebuild -pkg ctypes.foreign -lflags -cclib,-lecore_evas 
ecore_evas_window.native


But I have this error message :

+ ocamlfind ocamlc -c -w A-4-33-40-41-42-43-34-44 -strict-sequence -g 
-bin-annot -short-paths -thread -package ctypes.foreign -package core 
-ppx 'ppx-jane -as-ppx' -o ecore_evas_window.cmo ecore_evas_window.ml
File "ecore_evas_window.ml", line 4, characters 2-21:
Error: This expression has type int but an expression was expected of type
          unit
Command exited with code 2.
Hint: Recursive traversal of subdirectories was not enabled for this build,
   as the working directory does not look like an ocamlbuild project (no
   '_tags' or 'myocamlbuild.ml' file). If you have modules in 
subdirectories,
   you should add the option "-r" or create an empty '_tags' file.

   To enable recursive traversal for some subdirectories only, you can 
use the
   following '_tags' file:

       true: -traverse
       <dir1> or <dir2>: traverse

Compilation unsuccessful after building 4 targets (3 cached) in 00:00:00.

This message clearly show that the problem is related to the line 
ecore_evas_init (); but I don't really get it.


Any clues?


cedlemo

[email protected]

https://github.com/cedlemo