Version française
Home     About     Download     Resources     Contact us    
Browse thread
Initialize C library intarface
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Michel Pastor <K4ng@c...>
Subject: Initialize C library intarface
Hi,
I've a little problem with the initialization of my home made C
library binding for ocaml. Example:

***  BEGIN binding_stubs.c ***
#include <caml/mlvalues.h>

CAMLprim value   _binding_stubs_initialize(void) {
    /* intialization stuff */
}

CAMLprim value   _binding_stubs_dostuff(value data)
    CAMLparam1(data);
    /* do stuff with data */
    CAMLreturn(somevalue);
}
*** END binding_stubs.c ***

*** BEGIN binding.ml ***
external intialize : unit -> unit = "_binding_stubs_initialize";;
external dostuff : string -> sometype = "_binding_stubs_dostuff";;
let _ = initialize ();;
*** END binding.ml ***

*** BEGIN test.ml ***
open Binding;;
let _ = ignore (dostuff Sys.argv.(0));;
*** END test.ml ***

ocamlc -i binding.ml
ocamlc -c -cc gcc -ccopt '-fPIC -Wall' binding_stubs.c
ocamlc -c -w A binding.mli
ocamlc -c -w A binding.ml
ocamlmklib -o binding_stubs binding_stubs.o
ocamlc -a -dllib dllbinding_stubs.so -custom -cclib -lbinding_stubs -o 
binding.cma binding.cmo
ocamlopt -c -w A binding.ml
ocamlopt -a -cclib -lbinding_stubs -o binding.cmxa binding.cmx
ocamlc -o test -I . -I /sw/lib binding.cma test.ml
ocamlopt -o testx -I . -I /sw/lib binding.cmxa test.ml
ocamlmktop binding.cma -I . -o binding_caml

When I launch either the native or bytecode test application my 
initialization
code isn't executed but when I launch the ocaml top it is.
After some investigations I found that the initialization is executed in
the native and bytecode code only when I call a caml value from within
the binding module. My problem is that my binding is a pure C binding for
a C library so I don't have any "pure" caml value but only external 
declarations.
So I would know if it is possible to use a code similar to the example to
automatically call external C initialization in a pure "external" ocaml 
module
or if it is possible for that to be an ocaml bug.

thanks,
 - Michel