Using Make - with ocaml

How to use make with ocaml source files

Make can be a convenient way to compile, and recompile, ocaml sources.

Here is an example of a generic, compile rule, to compile an ocaml source file, "source.ml":

source.cmo: source.ml
        ocamlc -c source.ml

Put this compile reciepy in a file called Makefile, in the same directory than the file "source.ml".

Then running the command make, will execute the command:

ocamlc -c source.ml

And it will create the compiled bytecode object: "source.cmo"

When you edit the source file "source.ml", then you can automaticaly recompile the bytecode result, with the make command again.

It is also possible to create a generic reciepy-rule:

%.cmo: %.ml
        ocamlc -c $<

(replace the 8 spaces by a tab '\t')

The characters $< can be used for the input file, after the ':' colon character.

With this generic reciepy, you can call the command:

make source.cmo

It will compile the source-file, with the same basename, and the .ml, file extension.