Here are the steps to build ocaml files
The tool ocamlfind
simplifies the following:
- Searching for tools such as
ocamlopt
,ocamlc
- Querying existence of libraries for sanity checking
- Install/remove packages
- Use a toolchain
If you know location of tools and packages, feel free to type without ocamlfind
.
ocamlfind ocamlopt o002_hello_user.ml -o 002_hello_user.native
ocamlfind ocamlopt -linkpkg -package re.posix o001_grep_lines.ml -o 001_grep_lines.native
ocamlfind ocamlopt -i bintree.ml > bintree.mli
Feel free to edit the newly created .mli
file.
ocamlfind ocamlopt -c bintree.mli
If you have an abstract type, compile with the flag -opaque
or get warnings:
ocamlfind ocamlopt -c -opaque bintree.mli
ocamlfind ocamlopt -a bintree.ml -o bintree.cmxa
ocamlfind ocamlopt bintree.cmxa bintree_tests.ml -o bintree_tests.native
Read carefully the manual steps above and then you can write your own makfile.
Some examples of a Makefile
:
# OCaml build command
OBC = ocamlfind ocamlopt -linkpkg
OBCI = ocamlfind ocamlopt -c
OBCA = ocamlfind ocamlopt -a
bintree: bintree.ml bintree.mli bintree_tests.ml
$(OBCI) -opaque bintree.mli
$(OBCA) bintree.ml -o bintree.cmxa
$(OBC) bintree.cmxa bintree_tests.ml -o bintree_tests
001: o001_grep_lines.ml
$(OBC) -package re.posix o001_grep_lines.ml -o 001_grep_lines.native
002: o002_hello_user.ml
$(OBC) o002_hello_user.ml -o 002_hello_user.native
clean:
rm -rf *.o *.a *.lib
rm -rf *.cmi *.cmx *.cma *.cmxa
rm -rf *.bytes *.native
Note that ocamlbuild
still finds ocamlfind
useful and it can be used
Sample makefile that uses ocamlbuild
:
# Commands for the ocamlbuild interface
OCB_FLAGS = -use-ocamlfind
OCB = ocamlbuild $(OCB_FLAGS)
all: native byte
clean:
$(OCB) -clean
rm -rf *.o *.a *.lib
rm -rf *.cmi *.cmx *.cma *.cmxa
rm -rf *.bytes *.native _build
native: sanity
$(OCB) vfs_tests.native
byte: sanity
$(OCB) vfs_tests.byte
profile: sanity
$(OCB) -tag profile -package unix vfs_tests.native
debug: sanity
$(OCB) -tag debug -package unix vfs_tests.byte
sanity:
ocamlfind query unix
test: native
./vfs_tests.native
.PHONY: all clean native byte profile debug test default
Note that currently the commands profile
& debug
use -package unix
. You can keep them brief by moving required packages to the file _tags
in the same folder with the Makefile
.
The content of the file _tags
:
true: package(unix)
Now you can update Makefile
:
profile: sanity
$(OCB) -tag profile vfs_tests.native
debug: sanity
$(OCB) -tag debug vfs_tests.byte
For more info, read:
- https://github.com/ocaml/ocamlbuild/blob/master/manual/manual.adoc
- https://github.com/ocaml/ocamlbuild/blob/master/manual/examples/01-simple
- https://github.com/ocaml/ocamlbuild/blob/master/manual/examples/02-subdirs
- https://github.com/ocaml/ocamlbuild/blob/master/manual/examples/03-packages
- https://github.com/ocaml/ocamlbuild/blob/master/manual/examples/04-library
- https://github.com/ocaml/ocamlbuild/blob/master/manual/examples/05-lex-yacc
WIP, read more at: