I have a project structured like this:
Makefile
src/
main.ml
tests/
tests.ml
and the Makefile
is something like this:
tests:
ocamlbuild -Is src,tests tests.byte -build-dir $(BUILDDIR) $(TESTFLAGS) -lflags -I,/usr/lib/ocaml/oUnit -cflags -I,/usr/lib/ocaml/oUnit -libs oUnit
Running make tests
(after building main.byte
) returns this error:
ocamlbuild -Is src,tests tests.byte -build-dir build -lflags -I,/usr/lib/ocaml/oUnit -cflags -I,/usr/lib/ocaml/oUnit -libs oUnit
+ /usr/bin/ocamlc -c -I /usr/lib/ocaml/oUnit -I tests -I src -o tests/tests.cmo tests/tests.ml
File "tests/tests.ml", line 3, characters 46-50:
Error: Unbound value main
Command exited with code 2.
Compilation unsuccessful after building 2 targets (0 cached) in 00:00:00.
make: *** [tests] Error 10
showing that ocamlbuild
cannot link to main.byte
. What should the tests
rule of the Makefile
look like?
Since OCaml programs don't have a default main function (OCaml just runs the top-level code in each module at start-up) you'll probably want to have a separate file for starting the code. e.g.
Where:
main.ml
defines a main functionlet main args = ...
myprog.ml
just calls it:let () = Main.main Sys.argv
tests.ml
calls it in the same way from each test-caseThen build
myprog.byte
andtests.byte
as your two targets to ocamlbuild. Runmyprog.byte
to run the program andtests.byte
to run the tests.