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] mixing dynlink & toplevel...
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2006-08-27 (21:08)
From: Jonathan Roewen <jonathan.roewen@g...>
Subject: Re: [Caml-list] mixing dynlink & toplevel...
> Hi,
> I dunno if this is possible, but: if I use Dynlink, can I later load
> toplevellib.cma, and use it like normal? At the moment I get undefined
> reference to global 'Toploop'.
> Jonathan

I've just made a small test case to demonstate:

open Dynlink

let () = try
  print_endline "init";
  Dynlink.init ();
  print_endline "allowing unsafe modules";
  Dynlink.allow_unsafe_modules true;
  print_endline "loading toplevellib.cma";
  Dynlink.loadfile "/usr/local/lib/ocaml/toplevellib.cma";
  print_endline "loading topstart.cmo";
  Dynlink.loadfile "/usr/local/lib/ocaml/topstart.cmo";
  (* now the ocaml toplevel should be running *)
with Dynlink.Error error -> print_endline (Dynlink.error_message error);;

To compile: ocamlc -o test -linkall dynlink.cma test.ml

colinux:~/test# ./test
allowing unsafe modules
loading toplevellib.cma
loading topstart.cmo (shouldn't return)
        Objective Caml version 3.09.2

# let s = "hello world";;
Reference to undefined global `Toploop'
# exit 0;;

As you can see, it can run functions fine (e.g. calling
Pervasives.exit), but let bindings fail.

I know that a module loaded via Dynlink.loadfile can call functions
defined by other modules loaded by Dynlink.loadfile before it (I
actually do this), so why can't the toplevel access these? It can only
access those of the original program from my testing.