mingsterism
mingsterism

Reputation: 146

How to compile ocaml code to native to be used by C++

How can I compile this facebook parser library down to native code so it can be imported by my C++ program https://github.com/facebook/flow/tree/master/src/parser

I've tried using ocamlopt and dune build but there seems to be errors.

File "src/services/saved_state/dune", line 7, characters 4-29:
7 |     saved_state_dummy_fetcher
        ^^^^^^^^^^^^^^^^^^^^^^^^^
Error: Module Saved_state_dummy_fetcher is excluded but it doesn't exist.

Thanks

Upvotes: 1

Views: 445

Answers (1)

ivg
ivg

Reputation: 35210

OCaml is a different language from C or C++ and it uses different calling conventions and binary interface (ABI) than C or C++. Nothing special here about OCaml, even code compiled with different C++ compilers, in general, is not compatible and can't be imported.

So, if you want to call a library function written in language A from a program written in the language B you have to use bindings. If there are no bindings, then you can write your own, though it is not a trivial project as you have to decide how to translate C data structures to OCaml data structures and vice verse. If you or someone else will decide to pursue this route, here are some tips. The official documentation describes the low-level interface. A higher-level and less error-prone interface is provided by the CTypes library (which also can generate C code that you can later compile to a .so file).

Of course, before writing stubs you shall also learn how to compile the flow project, as the error that you show has nothing to do with importing OCaml code into C or C++ project. You should follow the instructions, provided by the flow homepage.

Upvotes: 1

Related Questions