ocamlbuild and OUnit

758 Views Asked by At

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?

2

There are 2 best solutions below

0
On

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.

main.ml
myprog.ml
tests.ml

Where:

  • main.ml defines a main function let main args = ...
  • myprog.ml just calls it: let () = Main.main Sys.argv
  • tests.ml calls it in the same way from each test-case

Then build myprog.byte and tests.byte as your two targets to ocamlbuild. Run myprog.byte to run the program and tests.byte to run the tests.

0
On

unbound value main does not sound like an error related to main.byte, but a genuine mistake in the OCaml code of tests.ml. Could you provide (possibly simplified) sources to experiment?

(Thomas' advice on program structure of course still stands, independently.)