简体   繁体   中英

ocamlbuild and OUnit

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. eg

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.

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM