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
dll from ocaml code
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-03-16 (12:56)
From: Matthieu Dubuget <matthieu.dubuget@g...>
Subject: Re: [Caml-list] dll from ocaml code
Joel Reymont a écrit :
> Does anyone have a recipe for packaging OCaml code as a DLL or shared
> library?


> I need to be able to expose a few entry points that correspond to
> OCaml functions.

Suppose that you add this line:

      flag ["link";"cmldll"] (S[A"-output-obj"]);

and this rule in the After_rules section of plugin:

    rule "Mixed C-Ocaml native DLL: cmldll & o* & cmx* -> native DLL
(.dll | .so)"
        ~prod:("%.native" -.- ext_dll)
          fun env build ->
            let output = env ("%.native" -.- ext_dll)
            and input = env "%.cmldll" in
            let dir = Pathname.dirname input in

            let ext_o_files, moduls_files =
              string_list_of_file input |>
                  List.partition (fun fic -> Filename.check_suffix fic
".o") in
            let objs = ext_o_files |>
       Filename.chop_extension |>
           (fun fic -> fic -.- ext_obj) in
            let cmxs = moduls_files |>
       (fun modul ->
                            (uncap_module_path modul) -.- "cmx") in
            let deps = cmxs @ objs in

              List.iter ignore_good
                (build ( (fun x -> [dir/x]) deps));

              Cmd (S [!Options.ocamlopt;
                      A"-o"; Px output;
                      T (tags_of_pathname
                      atomize_paths deps

where ext_dll is the dll file extension on your system,
ext_obj is tho obj files extension on your system,
let uncap_module_path p =
  (Pathname.dirname p) / (String.uncapitalize (Pathname.basename p)),

In order to build my.native.dll, you have to put in my.cmldll file:
- the ml Modules you want to link in your dll
- the associated obj files, obtained from C stubs.

And ocamlbuild my.native.dll will show you the working command lines…

Now, what to put in your C stub and Modules?

First, one module with your OCaml code.
Then, maybe a separated module that contains Callback.register calls, or
you have to had
them in to your module if you prefer.

Then the C part:

- first step is to init the caml part (see caml_startup in the manual),
either automatically upon
dll loading (in DllMain or _init function, depending on you system) or
in a function to be called
by the DLL client

- then write the C functions, that will calls your registered callbacks

I have no real example here around. But could find one somewhere on my
home's computer if

Hoping this helps