I want to compile my OCaml project into an executable that can be run in other computers that don't have OCaml installed. Using ocamlbuild, when I compile a ".native" file it works fine on other machines but if I compile a ".byte" file it fails with a Cannot exec ocamlrun
message when I try to run the executable.
Since the bytecode version of my program is significantly smaller in terms of file size, I would prefer to distribute it instead of the native code. Is there a way to bundle ocamlrun into the executable when I compile it?
You need to compile in a custom mode, from ocamlc user manual:
-custom
Link in “custom runtime” mode. In the default linking mode, the linker produces bytecode that is intended to be executed with the shared runtime system, ocamlrun. In the custom runtime mode, the linker produces an output file that contains both the runtime system and the bytecode for the program. The resulting file is larger, but it can be executed directly, even if the ocamlrun command is not installed. Moreover, the “custom runtime” mode enables static linking of OCaml code with user-defined C functions, as described in chapter
Unix: Never use the strip command on executables produced by ocamlc -custom, this would remove the bytecode part of the executable.
If you're using oasis
then all that you need is to add Custom : true
field to your executable section, similarly, for ocamlbuild
, add -tag custom
or put custom in _tags
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With