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
[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


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

    I wrote the two following programs : 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

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 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 Archives:
Bug reports: FAQ:
Beginner's list: