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

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-04-06 (07:46)
From: Pietro Abate <Pietro.Abate@a...>
Subject: Re: [Caml-list] dynlink
tnx for your help. below the working code for future reference...

On Tue, Apr 06, 2004 at 07:58:46AM +0200, Nicolas Cannasse wrote:
> (this should work, I have been using 2 years ago)

let modules = Hashtbl.create 17;;
let are_loading = Hashtbl.create 17;;
let load_path = ["."] ;;

let find_in_path path name =
  let filename = (String.uncapitalize name) ^ ".cmo" in
  if not (Filename.is_implicit filename) then
    if Sys.file_exists filename then filename else raise Not_found
  else begin
    let rec try_dir = function
        [] -> raise Not_found
        | dir::rem ->
                let fullname = Filename.concat dir filename in
                if Sys.file_exists fullname then fullname
                else try_dir rem
    in try_dir path

let rec load_module modname =
        Hashtbl.find modules modname
        Not_found ->
                Hashtbl.add modules modname ();
                Hashtbl.add are_loading modname ();
                Printf.printf "Loading: %s\n" modname ;
                Dynlink.loadfile (modname);
		(* register dynamic functions *)
                Hashtbl.remove are_loading modname;
            with Dynlink.Error(Dynlink.Unavailable_unit(depend))
                ) as e ->
                        if Hashtbl.mem are_loading depend
                        then failwith ("Crossing with "^depend);
                        load_module (find_in_path load_path depend);
                        Hashtbl.remove modules modname;
                        load_module modname

let _ =
	load_module (find_in_path load_path "modulename")

++ "All great truths begin as blasphemies." -George Bernard Shaw
++ Please avoid sending me Word or PowerPoint attachments.
   See http://www.fsf.org/philosophy/no-word-attachments.html

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