Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: Emmanuel Chailloux <Emmanuel.Chailloux@p...>
Subject: [Caml-list] Using the O'Caml toplevel inside a C program


  Hello,

     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 :

  g.ml
  -------

open Toploop;;
open Callback;;

print_string "apres les open";;
print_newline();;

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)";
   print_newline();
  ();;

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


p.c
----

#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);
   caml_startup(argv);

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

  ...

   v = copy_string("let x = ref 3;;");
   callback(*caml_named_value("exec_ocaml"), v);
   fflush(stdout);
  ...
   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 
`caml_startup_code'

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

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;
   Symtable.init_toplevel();
   Compile.init_path()
--
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