How to compile my OCaml code into a standalone bytecode executable?

I want to compile my OCaml project into an executable file that can be run on other computers that do not have OCaml installed. Using ocamlbuild, when I compile the ".native" file, it works fine on other machines, but if I compile the ".byte" file, it does not work with the Cannot exec ocamlrun when I try to run the executable.

Since the bytecode version of my program is much smaller in file size, I would prefer to distribute it instead of my own code. Is there a way to associate ocamlrun with an executable when compiling it?

+5
source share
1 answer

You need to compile in user mode, from the ocamlc manual user:

-custom

The link is in custom execution mode. In default binding mode, the linker creates bytecode that is designed to be executed using the common runtime, ocamlrun. In user runtime, the linker creates an output file containing 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 user-runtime mode provides static linking of OCaml code to C user functions, as described in the chapter

Unix: never use the strip command for executables created by ocamlc -custom, this will remove part of the bytecode executable.

If you use oasis , then you need to add the Custom : true field to your executable section, similarly for ocamlbuild , add -tag custom or put custom code in _tags .

+7
source

Source: https://habr.com/ru/post/1209451/


All Articles