English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

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: 2004-11-29 (05:31)
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 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 
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