* dll from ocaml code
@ 2009-03-16 11:21 Joel Reymont
2009-03-16 12:56 ` [Caml-list] " Matthieu Dubuget
0 siblings, 1 reply; 2+ messages in thread
From: Joel Reymont @ 2009-03-16 11:21 UTC (permalink / raw)
To: O'Caml Mailing List
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.
Thanks, Joel
---
http://tinyco.de
Mac, C++, OCaml
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [Caml-list] dll from ocaml code
2009-03-16 11:21 dll from ocaml code Joel Reymont
@ 2009-03-16 12:56 ` Matthieu Dubuget
0 siblings, 0 replies; 2+ messages in thread
From: Matthieu Dubuget @ 2009-03-16 12:56 UTC (permalink / raw)
Cc: O'Caml Mailing List
Joel Reymont a écrit :
> Does anyone have a recipe for packaging OCaml code as a DLL or shared
> library?
yes
>
> 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 myocamlbuild.ml plugin:
rule "Mixed C-Ocaml native DLL: cmldll & o* & cmx* -> native DLL
(.dll | .so)"
~dep:"%.cmldll"
~prod:("%.native" -.- ext_dll)
begin
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 |>
List.map Filename.chop_extension |>
List.map (fun fic -> fic -.- ext_obj) in
let cmxs = moduls_files |>
List.map (fun modul ->
(uncap_module_path modul) -.- "cmx") in
let deps = cmxs @ objs in
List.iter ignore_good
(build (List.map (fun x -> [dir/x]) deps));
Cmd (S [!Options.ocamlopt;
A"-o"; Px output;
T (tags_of_pathname
output++"ocaml"++"native"++"cmldll"++"link");
atomize_paths deps
])
end;
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
necessary.
Hoping this helps
Matt
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2009-03-16 12:56 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-03-16 11:21 dll from ocaml code Joel Reymont
2009-03-16 12:56 ` [Caml-list] " Matthieu Dubuget
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox