0

I want to implement shared library for Linux/FreeBSD in OCaml and I need it to have C interface. This way I will be able to implement bindings for various languages. I have several concerns:

  • OCaml is garbage-collected and I'm afraid that objects passed to external code may be GC'ed. Is it possible to disable GC? Maybe there is another way to solve such problems?
  • Is there a way to write OCaml code like C++'s extern "C" or alike so it will be exposed as C symbol?
  • Is there any similar high-level language which is better fit for writing solibs? I still need the language compiler to be written in C/C++ so it will be relatively easy to port it to new architectures.
  • 1
    There's [a section in the manual](https://caml.inria.fr/pub/docs/manual-ocaml/intfc.html) that deals with exactly this. You should start there. – glennsl Apr 25 '20 at 13:03
  • And your last question asks for opinion, which is frowned upon on SO because the format is ill fit for discussion. – glennsl Apr 25 '20 at 13:04
  • @glennsl , I read the manual but, as far as I understood, it suggests to wrap the library into separate C code instantiating OCaml and I'm trying to avoid such case. –  Apr 25 '20 at 13:43
  • What you're asking for is not possible IMHO. If you want the code to act exactly like C (linkages and memory allocation) you pretty much have to write in C. OCaml chooses its own names for functions, and avoiding GC will produce absurd looking OCaml for any non-trivial code. All of this is IMHO. – Jeffrey Scofield Apr 25 '20 at 16:27

1 Answers1

0

The thing I tried to accomplish is not possible the way mentioned. It was decided to expose the library via D-Bus because it is hard to implement bindings for tons of programming languages correctly but nowadays every language has batteries for D-Bus.

P. S.: Rust may be a good instrument for implementing C solib.