Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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
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)
    /* do stuff with data */
*** END binding_stubs.c ***

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

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

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

When I launch either the native or bytecode test application my 
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 
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 
or if it is possible for that to be an ocaml bug.

 - Michel