Yet another unbound value error

I started with an ocaml file Bataille.ml and finally decided to build a project with dune. The initial set up was as below :

Bataille
   |---> Bataille.ml

Then I added the dune project:

PS H:\Ocaml\Bataille> cd ..
PS H:\Ocaml> dune init proj Bataille

which ended up with the following setup (untouched folders not detailed):

Bataille
  |--_build
  |--bin
    |-->dune
    |-->main.ml
  |--lib
  |--test
  |-->Bataille.ml
  |-->Bataille.open
  |-->dune-project

I slightly modified the main.ml file so that it looks like that :

let monte_carlos () = 
  let n = 52 in
  let (jeu1,jeu2) = Bataille.init([],[],n) in
  List.iter jeu1 print_int;;

As highlighted by ocamllsp, when I run a dune build I get this error:

PS H:\OCaml\Bataille> dune build
File "bin/main.ml", line 4, characters 20-33:    
4 |   let (jeu1,jeu2) = Bataille.init([],[],n) in
                        ^^^^^^^^^^^^^
Error: Unbound value Bataille.init

Which, as a dune beginner (and ocaml beginner as well, I shall confess) am not able to deal with.
I fear an issue with capital letters here or there, but … this is a mystery for me. I have red somewhere I would not have to declare my modules with dune, it will cope with it … I must have missinterpreted something.

Help appreciated.

1 Like

For a simple executable, the lib and test directories are not needed (you can delete them). To fix your problem it should be enough to move Bataille.ml into the bin directory.

Cheers,
Nicolas

Thank you Nicolas, it worked. Not used to put source code in bin directory but if that’s the way.

Putting the sources of your executables in the bin/ directory is just a convention. You are free to put them anywhere you want (even at the root). As long as all the sources and the dune file are in the same directory, Dune should have no trouble building your binary.

Cheers,
Nicolas

1 Like

Just a note that for a simple beginner project you don’t necessarily need to use dune init project, which sets up a general-purpose project structure that’s really more suitable for moderately sized projects. You can just write a couple of dune config files by hand, as shown here: https://dune.build/