caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Tutorial example for nat dynlink
@ 2008-11-26 14:22 Yves Bertot
  2008-11-26 14:34 ` [Caml-list] " Xavier Leroy
  0 siblings, 1 reply; 2+ messages in thread
From: Yves Bertot @ 2008-11-26 14:22 UTC (permalink / raw)
  To: caml-list

Hello,

I have been trying to build a small example "hello world" program using 
native dynlink, but
I don't manage to make it work.  Could please send me some advice?


Here are my programs:

-------- toto.mli ----
val ff : (unit -> unit) ref;;
-------- toto.ml -----
Dynlink.init();;

let ff = ref (fun () -> ());;

try Dynlink.loadfile "bidon.cmxs" with Dynlink.Error(e) ->
  print_string (Dynlink.error_message e ^ "\n");;

!ff();;
------ bidon.ml -----
let hello () = print_string "hello world!\n";;

Toto.ff := hello;;
------ end of files ------

ocamlopt -v returns

The Objective Caml native-code compiler, version 3.11.0+beta1
Standard library directory:  .../lib/ocaml

And I compile my files in the following manner.

ocamlopt toto.mli
ocamlopt -shared -o bidon.cmxs bidon.ml
ocamlopt -o toto dynlink.cmxa toto.ml

My test runs as follows, with a behavior I did not expect:

./toto
no implementation available for Toto

Can you tell me what is missing in my experiment, or point me to an 
example that would be about the same size?

Yves




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] Tutorial example for nat dynlink
  2008-11-26 14:22 Tutorial example for nat dynlink Yves Bertot
@ 2008-11-26 14:34 ` Xavier Leroy
  0 siblings, 0 replies; 2+ messages in thread
From: Xavier Leroy @ 2008-11-26 14:34 UTC (permalink / raw)
  To: Yves Bertot; +Cc: caml-list

Hello Yves,

> I have been trying to build a small example "hello world" program
> using native dynlink, but I don't manage to make it work.  Could
> please send me some advice?

The issue here is that when Dynlink.loadfile is called, module Toto is
not yet completely evaluated, therefore it is not yet visible to
dynamically-loaded code.  The solution is just to split your main
program in two modules: Toto containing what your plugin needs to see,
and Totomain performing the dynlink.  Then, it'll work just fine.

Cheers,

- Xavier


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2008-11-26 14:34 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-11-26 14:22 Tutorial example for nat dynlink Yves Bertot
2008-11-26 14:34 ` [Caml-list] " Xavier Leroy

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).