caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Emmanuel Chailloux <Emmanuel.Chailloux@pps.jussieu.fr>
To: caml-list@inria.fr
Subject: [Caml-list] Using the O'Caml toplevel inside a C program
Date: Fri, 10 May 2002 16:09:51 +0200	[thread overview]
Message-ID: <98219074-641F-11D6-B61B-0050E4C56074@pps.jussieu.fr> (raw)
In-Reply-To: <20020509185105.P1213@speakeasy.org>



  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


  reply	other threads:[~2002-05-10 14:10 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-05-09 17:55 [Caml-list] generating random variables Henri Dubois-Ferriere
2002-05-09 18:22 ` David Chase
2002-05-09 18:45 ` Olivier Andrieu
2002-05-10  1:51 ` Shawn Wagner
2002-05-10 14:09   ` Emmanuel Chailloux [this message]
2002-05-14  8:59     ` [Caml-list] Using the O'Caml toplevel inside a C program Xavier Leroy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=98219074-641F-11D6-B61B-0050E4C56074@pps.jussieu.fr \
    --to=emmanuel.chailloux@pps.jussieu.fr \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).