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
[Caml-list] generating random variables
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-05-10 (14:10)
From: Emmanuel Chailloux <Emmanuel.Chailloux@p...>
Subject: [Caml-list] Using the O'Caml toplevel inside a C program


     I try to use the O'Caml toplevel inside a C program but the result 
does not run.

    I wrote the two following programs : g.ml and p.c :


open Toploop;;
open Callback;;

print_string "apres les open";;

let exec s =
   let ast = !parse_toplevel_phrase (Lexing.from_string s) in
   ignore(execute_phrase false Format.std_formatter ast);;

let main() =
   print_string "lancement de main (OCAML)";

Callback.register "exec_ocaml" exec;;
Callback.register "main_ocaml" main;;


#include <stdlib.h>
#include <stdio.h>

#include <caml/mlvalues.h>
#include <caml/callback.h>

int main ( int argc, char ** argv ) {
   value v;
   printf("debut de C\n"); fflush(stdout);

   printf("init OCAML\n"); fflush(stdout);
   callback(*caml_named_value("main_ocaml"), Val_unit);


   v = copy_string("let x = ref 3;;");
   callback(*caml_named_value("exec_ocaml"), v);
   return EXIT_SUCCESS;

compilation (MacOSX) :
$ ocamlc -output-obj toplevellib.cma unix.cma g.ml

camlprog.c: In function `caml_startup':
camlprog.c:39186: warning: implicit declaration of function 

$ cc -I/usr/local/lib/ocaml -L/usr/local/lib/ocaml camlprog.o p.c -lunix 

execution (MacOSX, but I obtain the same behaviour with Linux)  :
  $ ./a.out
debut de C
 >> Fatal error: Toplevel bytecode executable is corrupted
Fatal error: exception Misc.Fatal_error

I suppose that the problem comes from the toploop.ml file in the 
following expression :

let _ =
   Sys.interactive := true;
but I'am not sure.

  Is there a solution or another way to do that?

Best regards.
Emmanuel Chailloux...

To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners