You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Could we enable ocamlbuild to build dynamic libraries (.so/.dll)
that are accessible via a C-API which internally uses OCaml code?
We have such a setup, where we need to provide a C-API to the outside
and use OCaml to implement the functionality.
The request is somehow the reverse of the C-stubs build:
We are not looking for a means to build stubs to C-libraries,
but to wrap OCaml code into a C-API.
Additional information
The custom myocamlbuild.ml (admittedly hacked and ugly) that does the job for our setup in Windows is as follows:
open Ocamlbuild_plugin
open Command
(** This function implements the action for building the native C library *)
let library_build env (_build : builder) =
let lib_main = env "%.cmxa" in
let stub_obj = env "%_api.obj" in
let final_link_cmd = Cmd (S [ S[A "ocamlfind"; A "ocamlopt";
S[A "-package"; A "ocamlgraph"];
S[A "-package"; A "batteries"];
S[A "-cclib"; A "-implib"];
S[A "-cclib"; A "-maindll"];
A "-output-obj"; A "-linkpkg"; A "-linkall"]; S [A "-o"; A "ourlibrary.dll"]; P stub_obj; P lib_main]) in
let pn_lib_main = Pathname.mk lib_main in
let pn_stub_main = Pathname.mk stub_obj in
let _ = _build [[pn_lib_main]; [pn_stub_main]] in
final_link_cmd
let () =
dispatch (function
| After_rules ->
rule "Building C-DLL from OCaml with a C-API"
~prod:"%.clibrary"
~deps:["%.ml"; "%_api.c"; "%.h"]
library_build;
flag ["clibrary"] (A "-linkpkg");
tag_file "</.so>" ["clibrary"]
| _ -> ())
The text was updated successfully, but these errors were encountered:
PR6733 and PR6797 address parts of the issue I was reporting.
However, we additionally miss a way to tell ocamlbuild to compile specific *.c files and link the resulting object code into the final .dll/.so
The background is as follows:
We use C to implement an API that wraps the OCaml core of our library.
Is there a possibility to make ocamlbuild compile and link the
additional C files that contain the wrapper code?
An approach that is similar to the *.clib files that are used for compiling C-stubs would be great.
However, perhaps I overlook something and such a build configuration
becomes available due to PR6733 and PR6797?
Original bug ID: 6918
Reporter: sawfish
Status: resolved (set by @damiendoligez on 2017-03-01T12:18:19Z)
Resolution: suspended
Priority: normal
Severity: feature
Version: 4.02.2
Target version: later
Category: -for ocamlbuild use https://github.com/ocaml/ocamlbuild/issues
Tags: patch
Related to: #6733 #6797
Monitored by: @whitequark @dbuenzli
Bug description
Could we enable ocamlbuild to build dynamic libraries (.so/.dll)
that are accessible via a C-API which internally uses OCaml code?
We have such a setup, where we need to provide a C-API to the outside
and use OCaml to implement the functionality.
The request is somehow the reverse of the C-stubs build:
We are not looking for a means to build stubs to C-libraries,
but to wrap OCaml code into a C-API.
Additional information
The custom myocamlbuild.ml (admittedly hacked and ugly) that does the job for our setup in Windows is as follows:
open Ocamlbuild_plugin
open Command
(** This function implements the action for building the native C library *)
let library_build env (_build : builder) =
let lib_main = env "%.cmxa" in
let stub_obj = env "%_api.obj" in
let final_link_cmd = Cmd (S [ S[A "ocamlfind"; A "ocamlopt";
S[A "-package"; A "ocamlgraph"];
S[A "-package"; A "batteries"];
S[A "-cclib"; A "-implib"];
S[A "-cclib"; A "-maindll"];
A "-output-obj"; A "-linkpkg"; A "-linkall"]; S [A "-o"; A "ourlibrary.dll"]; P stub_obj; P lib_main]) in
let pn_lib_main = Pathname.mk lib_main in
let pn_stub_main = Pathname.mk stub_obj in
let _ = _build [[pn_lib_main]; [pn_stub_main]] in
final_link_cmd
let () =
dispatch (function
| After_rules ->
rule "Building C-DLL from OCaml with a C-API"
~prod:"%.clibrary"
~deps:["%.ml"; "%_api.c"; "%.h"]
library_build;
flag ["clibrary"] (A "-linkpkg");
tag_file "</.so>" ["clibrary"]
| _ -> ())
The text was updated successfully, but these errors were encountered: